package com.yuncheng.oaApi.bxdService;

import cn.hutool.core.collection.CollectionUtil;
import com.yuncheng.constant.SysUserConstant;
import com.yuncheng.entity.YcSysOrg;
import com.yuncheng.entity.YcSysUser;
import com.yuncheng.oaApi.vo.*;
import com.yuncheng.spcyApi.entity.SpcySqsxWjjl;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.spcyApi.service.ISpcySqsxWjjlService;
import com.yuncheng.spcyApi.utils.DateConvert;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.utils.StringPatternUtils;
import com.yuncheng.utils.ThreadContextHelper;
import org.apache.commons.collections.CollectionUtils;
import com.yuncheng.oaApi.constant.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.oaApi.entity.*;
import com.yuncheng.exception.BusinessException;

import com.yuncheng.oaApi.mapper.OaSpcyJcyFybxdMapper;
import com.yuncheng.oaApi.service.*;
import com.yuncheng.oaApi.utils.*;
import com.yuncheng.vo.*;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import utils.DateConversionUtils;
import utils.LogUtils;
import utils.ObjectToMapUtils;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.*;

/**
 * @author hua
 */
@Component(value = "oajcyFybxdService")
public class OaJcyFybxdService {

    private static final Logger log = LoggerFactory.getLogger(OaJcyFybxdService.class);

    @Resource
    private IOaSpcyJcyFybxdgrjlService spcyJcyFybxdgrjlService;


    @Resource
    @Lazy
    private INkRsCcspService nkRsCcspService;


    @Resource
    @Lazy
    private IOaBgypsqglService oaBgypsqglService;

    @Resource
    @Lazy
    private IOaXzswspEntityService oaxzswspService;

    @Resource
    private IOaSpcyJcyFybxdService spcyJcyFybxdService;

    @Resource
    private OaSpcyJcyFybxdMapper oaSpcyJcyFybxdMapper;

    @Resource
    private IOaSpcyJcyFybxdtssxsqService spcyJcyFybxdtssxsqService;


    @Resource
    private ISpcySqsxWjjlService spcySqsxWjjlService;

    @Resource
    private IOaYcSysUserService sysUserService;

    @Resource
    private ISpcyLogService spcyLogService;

    @Resource
    @Lazy
    private IOaYcSysUserService ycSysUserService;

    @Resource
    @Lazy
    private IOaYcSysOrgService ycSysOrgService;

    @Resource
    @Lazy
    private IOaSpcyJcyFybxxmmxjlService spcyJcyFybxxmmxkaslService;

    @Resource
    @Lazy
    private INkRsXmyszbbService nkRsXmyszbbService;

    @Resource
    @Lazy
    private IOaNkRsDjhglService nkRsDjhglService;

    @Resource
    @Lazy
    private IOaSpcySqsxJobService spcySqsxJobService;

    @Resource
    @Lazy
    private INkRsZzsqdService nkRsZzzsqdService;



    @Resource
    @Lazy
    private INkRsBxqtzyjlService nkRsBxqtzyjlService;


    @Resource
    @Lazy
    private INkBxJtxcdService nkBxJtxcdService;

    @Resource
    @Lazy
    private INkDgzzgslbService ndgzzgslbService;


    @Resource
    @Lazy
    private INkRsHtspjlService nkRsHtspjlService;

    @Resource
    @Lazy
    private INkJcxswsqService nkJcxswsqService;


    /**
     * 初始化申请事项-费用报销单
     * @param sqsxid
     */
    public Result initSqsxFybxd(String sqsxid, String flcbm, String bxlx, String sfdgzz) throws Exception {
        OaSpcyJcyFybxd fybxd = new OaSpcyJcyFybxd();
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        String bxnrms = "";
        String bxsjms = "";

        //检查天数/天数
        int jcts = 0;
        //开始时间
        String kssj = "";
        //结束时间
        String jssj = "";
        //报销单类型
        String bxdlx = "";
        List<String> sqsxidlist = Arrays.asList(sqsxid.split(",|，"));

        //经办人信息，是拿申请单的数据人，还是拿当前登陆人的数据
        //当前填写报销单的申请人信息,从当前申请用户拿到
        String orgId = ycSysUserService.getUserZbmByUserId(currentUser.getId());
        YcSysOrg org = ycSysOrgService.getById(orgId);
        String phone = ycSysUserService.getById(currentUser.getId()).getPhone();

        if (StringUtils.isBlank(sqsxid) && StringUtils.isBlank(flcbm) ){
           if (bxlx.equals(OaJcyFybxdConstant.STATE_CLBX)){
                fybxd.setId(GetUuIdUtils.ReplaceUuId());
                fybxd.setFsqsxid(sqsxid);

                //经办人个人信息
                fybxd.setFjbrid(currentUser.getId());
                fybxd.setFjbr(currentUser.getName());
                fybxd.setFjbksid(orgId);
                fybxd.setFjbks(org.getOrgName());
                fybxd.setFlxdh(phone);

                //报销内容
                fybxd.setFjcsxmc("");
                fybxd.setFjcksrq("");
                fybxd.setFjcjsrq("");
                fybxd.setFjcts(null);

                //流程
                fybxd.setFsffqlc(OaCommonFlowConstant.FSFFQLC_NO);

                //金额
                fybxd.setFhjbxje("0"); // 合计报销金额
                fybxd.setFlcbm("");
                fybxd.setFbxnrms("");

                fybxd.setFbxlx(bxlx);
                fybxd.setFsfdgzz(OaJcyFybxdConstant.STATE_SFDGZZ_NO);

                fybxd.setFsfywdwpc(sfdgzz);

                fybxd.setFdjh(nkRsDjhglService.GetDocumentNumber(fybxd.getId()));
                fybxd.setFbxdlx(OaJcyFybxdConstant.FYBXD_ZZRYCCBXD);
                fybxd.setFtbrq(new Date());
                fybxd.setFbxdzt(OaJcyFybxdConstant.getFirstState());
                fybxd.setFsqyj("");
                spcyJcyFybxdService.save(fybxd);
           }else if(bxlx.equals(OaJcyFybxdConstant.STATE_YBBX)){
//               if (sfdgzz.equals(JcyFybxdConstant.STATE_SFDGZZ_YES)){
//                   //如果报销类型为一般报销，并且是否对公转账为是：那就是对公转账申请单
//                   //对公转账申请单有 ：申请依据 和 合同申请单
//                   fybxd.setId(GetUuIdUtils.ReplaceUuId());
//                   fybxd.setFsqsxid(sqsxid);
//
//                   //经办人个人信息
//                   fybxd.setFjbrid(currentUser.getId());
//                   fybxd.setFjbr(currentUser.getName());
//                   fybxd.setFjbksid(orgId);
//                   fybxd.setFjbks(org.getOrgName());
//                   fybxd.setFlxdh(phone);
//
//                   //报销内容
//                   fybxd.setFjcsxmc("");
//                   fybxd.setFjcksrq("");
//                   fybxd.setFjcjsrq("");
//                   fybxd.setFjcts(null);
//
//                   //流程
//                   fybxd.setFsffqlc(CommonFlowConstant.FSFFQLC_NO);
//
//                   //金额
//                   fybxd.setFhjbxje("0"); // 合计报销金额
//                   fybxd.setFlcbm("");
//                   fybxd.setFbxnrms("");
//
//                   fybxd.setFbxlx(bxlx);
//                   fybxd.setFsfdgzz(JcyFybxdConstant.STATE_SFDGZZ_YES);
//
//                   fybxd.setFsfywdwpc(sfdgzz);
//                   fybxd.setFbxdlx("");
//                   fybxd.setFdjh(nkRsDjhglService.GetDocumentNumber(fybxd.getId()));
//                   fybxd.setFbxdlx(JcyFybxdConstant.FYBXD_DGZZSQD);
//                   fybxd.setFtbrq(new Date());
//                   fybxd.setFbxdzt(JcyFybxdConstant.getFirstStates());
//                   fybxd.setFsqyj("");
//                   spcyJcyFybxdService.save(fybxd);
//
//               }else
//               if(sfdgzz.equals(JcyFybxdConstant.STATE_SFDGZZ_NO)){
                   fybxd.setId(GetUuIdUtils.ReplaceUuId());
                   fybxd.setFsqsxid(sqsxid);

                   //经办人个人信息
                   fybxd.setFjbrid(currentUser.getId());
                   fybxd.setFjbr(currentUser.getName());
                   fybxd.setFjbksid(orgId);
                   fybxd.setFjbks(org.getOrgName());
                   fybxd.setFlxdh(phone);

                   //报销内容
                   fybxd.setFjcsxmc("");
                   fybxd.setFjcksrq("");
                   fybxd.setFjcjsrq("");
                   fybxd.setFjcts(null);

                   //流程
                   fybxd.setFsffqlc(OaCommonFlowConstant.FSFFQLC_NO);

                   //金额
                   fybxd.setFhjbxje("0"); // 合计报销金额
                   fybxd.setFlcbm("");
                   fybxd.setFbxnrms("");

                   fybxd.setFbxlx(bxlx);
                   fybxd.setFsfdgzz(OaJcyFybxdConstant.STATE_SFDGZZ_NO);

                   fybxd.setFsfywdwpc(sfdgzz);
                   fybxd.setFbxdlx("");
                   fybxd.setFdjh(nkRsDjhglService.GetDocumentNumber(fybxd.getId()));
//                   fybxd.setFbxdlx(JcyFybxdConstant.FYBXD_ZCBXD);
                   fybxd.setFtbrq(new Date());
                   fybxd.setFbxdzt(OaJcyFybxdConstant.getFirstStates());
                   fybxd.setFsqyj("");
                   spcyJcyFybxdService.save(fybxd);

//               }
           }
        }else {
            List sxlist = new ArrayList();
            //初始化基本信息
            if (StringUtils.isBlank(flcbm)) {
                throw new BusinessException("流程编码为空！");

            } else if (flcbm.equals(OaFlowConstant.NK_CCSP)) {
                List<NkRsCcsp> nkRsCcsps = nkRsCcspService.listByIds(sqsxidlist);
                //将事项添加进list为后续做判断
                sxlist.add(nkRsCcsps);
                if (CollectionUtil.isEmpty(sxlist)) {
                    throw new BusinessException("没有相匹配的数据！");
                }
                //报销单类型
                bxdlx = OaJcyFybxdConstant.FYBXD_ZZRYCCBXD;
                //在这里一次性处理完，返回后续所有需要数据.多个申请事项只拿第一个的数据
                //报销内容描述:
                bxnrms = nkRsCcsps.get(0).getFccsy();

                //天数
                String fccksrq = nkRsCcsps.get(0).getFccksrq();
                String fccjsrq = nkRsCcsps.get(0).getFccjsrq();
                kssj = fccksrq;
                jssj = fccjsrq;

                long day = DateConversionUtils.dateDiffWithDay(fccksrq, fccjsrq);
                jcts = (int) day;
                //报销时间描述
                bxsjms = fccksrq + "至" + fccjsrq + "共" + jcts + "天";

            } else if (flcbm.equals(OaFlowConstant.OA_BGYPSQ)) {
                List<OaBgypsqgl> oaBgypsqgls = oaBgypsqglService.listByIds(sqsxidlist);

                sxlist.add(oaBgypsqgls);
                if (CollectionUtil.isEmpty(sxlist)) {
                    throw new BusinessException("没有相匹配的数据！");
                }
                //报销内容描述
                bxnrms = oaBgypsqgls.get(0).getFlyks() + "对" + oaBgypsqgls.get(0).getFpm() + "申请" + oaBgypsqgls.get(0).getFsl() + oaBgypsqgls.get(0).getFdw();
                //报销时间描述为填报日期
                bxsjms = oaBgypsqgls.get(0).getFlyrq();
            } else if (flcbm.equals(OaFlowConstant.OA_XZSWSP)) {
                List<OaXzswspEntity> oaXzswspEntities = oaxzswspService.listByIds(sqsxidlist);
                sxlist.add(oaXzswspEntities);
                if (CollectionUtil.isEmpty(sxlist)) {
                    throw new BusinessException("没有相匹配的数据！");
                }
                //报销内容描述
                bxnrms = oaXzswspEntities.get(0).getFsx();
                //报销时间描述为填报日期
                bxsjms = oaXzswspEntities.get(0).getFrq();
            } else if (flcbm.equals(OaFlowConstant.NK_HTSPLC)){
                List<NkRsHtspjl> nkRsHtspjls = nkRsHtspjlService.listByIds(sqsxidlist);
                sxlist.add(nkRsHtspjls);
                if (CollectionUtil.isEmpty(sxlist)) {
                    throw new BusinessException("没有相匹配的数据！");
                }

                bxnrms = nkRsHtspjls.get(0).getFhtmc();
                bxsjms = nkRsHtspjls.get(0).getFtxsj();


            }
            //使用in来包含多个id
            List<OaSpcyJcyFybxd> bxdList = spcyJcyFybxdService.list(new LambdaQueryWrapper<OaSpcyJcyFybxd>()
                    .in(OaSpcyJcyFybxd::getFsqsxid, sqsxid));

            if (CollectionUtil.isNotEmpty(bxdList)) {
                throw new BusinessException("已经存在对应的报销单信息！");
            }

            fybxd.setId(GetUuIdUtils.ReplaceUuId());
            fybxd.setFsqsxid(sqsxid);

            //经办人个人信息
            fybxd.setFjbrid(currentUser.getId());
            fybxd.setFjbr(currentUser.getName());
            fybxd.setFjbksid(orgId);
            fybxd.setFjbks(org.getOrgName());
            fybxd.setFlxdh(phone);

            //报销内容
            fybxd.setFjcsxmc(bxnrms);
            fybxd.setFjcksrq(kssj);
            fybxd.setFjcjsrq(jssj);
            fybxd.setFjcts(jcts);
            fybxd.setFbxlx(bxlx);
            fybxd.setFsffqlc(OaCommonFlowConstant.FSFFQLC_NO);
            //金额
            fybxd.setFhjbxje("0"); // 合计报销金额
            fybxd.setFlcbm(flcbm);
            fybxd.setFbxnrms(bxnrms);
            fybxd.setFbxsjms(bxsjms);

            fybxd.setFdjh(nkRsDjhglService.GetDocumentNumber(fybxd.getId()));
            fybxd.setFtbrq(new Date());
            fybxd.setFbxdzt(OaJcyFybxdConstant.getFirstState());
            fybxd.setFsqyj(getsqyjList(sqsxid, flcbm));
            fybxd.setFbxdlx(OaJcyFybxdConstant.FYBXD_ZZRYCCBXD);

            fybxd.setFsfywdwpc(sfdgzz);
            spcyJcyFybxdService.save(fybxd);

            this.initJcyFybxdGrjl(sqsxid, flcbm, fybxd); // 初始化-报销单个人记录
        }
        return  Result.ok(fybxd);
    }


    /**
     * 根据申请依据新增报销单信息
     */
    public String updateJcyFybxdGrjl(String bxdid,String sqsxid,String flcbm) throws Exception {
        String bxnrms = "";
        String bxsjms = "";

        //检查天数/天数
        int jcts = 0;
        //开始时间
        String kssj = "";
        //结束时间
        String jssj = "";
        OaSpcyJcyFybxd oaSpcyJcyFybxd = spcyJcyFybxdService.getById(bxdid);
        String fsqsxid = "";
        String fsqyj = "";
        if (oaSpcyJcyFybxd == null){
            throw new BusinessException("未查询到对应的报销单信息");
        }else{
            String oldfsqsxid = oaSpcyJcyFybxd.getFsqsxid();
            String oldfsqyj = oaSpcyJcyFybxd.getFsqyj();
            fsqsxid = StringUtils.isNotBlank(oldfsqsxid) ? oldfsqsxid + "," : "";
            fsqyj = StringUtils.isNotBlank(oldfsqyj) ? oldfsqyj + "," : "";
            oaSpcyJcyFybxd.setFsqsxid(fsqsxid + sqsxid);

            oaSpcyJcyFybxd.setFsqyj(fsqyj+getsqyjList(sqsxid,flcbm));
            oaSpcyJcyFybxd.setFlcbm(flcbm);
            oaSpcyJcyFybxd.setFbxnrms(getFnrms(sqsxid,flcbm));
            List<String> sqsxidlist = Arrays.asList(sqsxid.split(",|，"));
            if (flcbm.equals(OaFlowConstant.NK_CCSP)){
                List<NkRsCcsp> nkRsCcsps = nkRsCcspService.listByIds(sqsxidlist);
                bxnrms = nkRsCcsps.get(0).getFccsy();

                //天数
                String fccksrq = nkRsCcsps.get(0).getFccksrq();
                String fccjsrq = nkRsCcsps.get(0).getFccjsrq();
                kssj = fccksrq;
                jssj = fccjsrq;

                long day = DateConversionUtils.dateDiffWithDay(fccksrq, fccjsrq);
                jcts = (int) day;
                //报销时间描述
                bxsjms = fccksrq + "至" + fccjsrq + "共" + jcts + "天";
            }else if (flcbm.equals(OaFlowConstant.OA_BGYPSQ)){
                List<OaBgypsqgl> oaBgypsqgls = oaBgypsqglService.listByIds(sqsxidlist);
                //报销内容描述
                bxnrms = oaBgypsqgls.get(0).getFlyks() + "对" + oaBgypsqgls.get(0).getFpm() + "申请" + oaBgypsqgls.get(0).getFsl() + oaBgypsqgls.get(0).getFdw();


            }else if(flcbm.equals(OaFlowConstant.OA_XZSWSP)){
                List<OaXzswspEntity> oaXzswspEntities = oaxzswspService.listByIds(sqsxidlist);
                //报销内容描述
                bxnrms = oaXzswspEntities.get(0).getFsx();

                bxsjms = DateConversionUtils.DateToStringYYYYMMDD(new Date());

            }else if (flcbm.equals(OaFlowConstant.NK_HTSPLC)){

                List<NkRsHtspjl> nkRsHtspjls = nkRsHtspjlService.listByIds(sqsxidlist);
                NkRsHtspjl nkRsHtspjl = nkRsHtspjls.get(0);
                bxnrms = nkRsHtspjl.getFtxr() + "对" + nkRsHtspjl.getFhtmc();
                bxsjms = DateConversionUtils.DateToStringYYYYMMDD(new Date());
            }else if (flcbm.equals(OaFlowConstant.NK_JCXSWSQ)){
                List<NkJcxswsq> nkJcxswsqs = nkJcxswsqService.listByIds(sqsxidlist);
                NkJcxswsq nkJcxswsq = nkJcxswsqs.get(0);
                bxnrms = nkJcxswsq.getFsqr() + "对" + nkJcxswsq.getFsqnr();
                bxsjms = DateConversionUtils.DateToStringYYYYMMDD(new Date());
            }
            //报销内容
            oaSpcyJcyFybxd.setFjcsxmc(bxnrms);
            oaSpcyJcyFybxd.setFjcksrq(kssj);
            oaSpcyJcyFybxd.setFjcjsrq(jssj);
            oaSpcyJcyFybxd.setFjcts(jcts);
            oaSpcyJcyFybxd.setFbxsjms(bxsjms);


            spcyJcyFybxdService.updateById(oaSpcyJcyFybxd);
            this.updateJcyFybxdGrjl(sqsxid,flcbm, oaSpcyJcyFybxd);
            return oaSpcyJcyFybxd.getFsqsxid();
        }
    }

    private void updateJcyFybxdGrjl(String sqsxid,String flcbm, OaSpcyJcyFybxd fybxd){

        CurrentUserVo currentUser = ThreadContextHelper.getCurrentUser();
        List<String> idList = Arrays.asList(sqsxid.split(",|，"));
        //根据流程编码来生成对应的费用报销单人记录
        if(flcbm.equals(OaFlowConstant.NK_CCSP)){
            List<NkRsCcsp> nkRsCcsps = nkRsCcspService.listByIds(idList);
            for (int order = 0; order < nkRsCcsps.size(); order++){
                YcSysUser user = sysUserService.getById(nkRsCcsps.get(order).getFuserid());
                if (user != null) {
                    //2024-5-6 ：出差审批同行人是否为空，如果同行人为空那就只有一个，如果不为空 那就循环同行人字段添加报销单个人记录
                    if (StringUtils.isBlank(nkRsCcsps.get(order).getFcctxryid())){
                        updateFybxdGrjl(nkRsCcsps.get(order).getId(),user, fybxd);
                    }else {
                        //先新增本人的报销单个人记录
                        updateFybxdGrjl(nkRsCcsps.get(order).getId(),user, fybxd);
                        String[] cctxryids = nkRsCcsps.get(order).getFcctxryid().split(",");
                        for (int i = 0; i < cctxryids.length; i++){
                            YcSysUser users = sysUserService.getById(cctxryids[i]);
                            if (users != null) {
                                updateFybxdGrjl(nkRsCcsps.get(order).getId(),users, fybxd);
                            }
                        }
                    }

//                    updateFybxdGrjl(nkRsCcsps.get(order).getId(),user, fybxd);

                }
            }
        }else if(flcbm.equals(OaFlowConstant.OA_BGYPSQ)){
            List<OaBgypsqgl> oaBgypsqgls = oaBgypsqglService.listByIds(idList);
            for (int order = 0; order < oaBgypsqgls.size(); order++){
                YcSysUser user = sysUserService.getById(oaBgypsqgls.get(order).getFuserid());
                if (user != null) {
                    updateFybxdGrjlByDg(oaBgypsqgls.get(order).getId(),user, fybxd);
                }
            }
        }else if(flcbm.equals(OaFlowConstant.OA_XZSWSP)){
            List<OaXzswspEntity> oaXzswspEntities = oaxzswspService.listByIds(idList);
            for (int order = 0; order < oaXzswspEntities.size(); order++){
                YcSysUser user = sysUserService.getById(currentUser.getId());
                if (user != null) {
                    updateFybxdGrjlByDg(oaXzswspEntities.get(order).getId(),user, fybxd);
                }
            }
        }else if (flcbm.equals(OaFlowConstant.NK_HTSPLC)){
            List<NkRsHtspjl> nkRsHtspjls = nkRsHtspjlService.listByIds(idList);
            for (int order = 0; order < nkRsHtspjls.size(); order++){
                YcSysUser user = sysUserService.getById(currentUser.getId());
                if (user != null) {
                    updateFybxdGrjlByDg(nkRsHtspjls.get(order).getId(),user, fybxd);
                }
            }

        }else if (flcbm.equals(OaFlowConstant.NK_JCXSWSQ)){
            List<NkJcxswsq> nkJcxswsqs = nkJcxswsqService.listByIds(idList);
            for (int order = 0; order < nkJcxswsqs.size(); order++){
                YcSysUser user = sysUserService.getById(currentUser.getId());
                if (user != null) {
                    updateFybxdGrjlByDg(nkJcxswsqs.get(order).getId(),user, fybxd);
                }
            }
        }

    }

    private void updateFybxdGrjl(String sqsxid,YcSysUser user, OaSpcyJcyFybxd fybxd){
        List<OaSpcyJcyFybxdgrjl> mxList = spcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, fybxd.getId())
                .eq(OaSpcyJcyFybxdgrjl::getFuserid, user.getId()));
        if (CollectionUtil.isNotEmpty(mxList)){
            return;
        }

        OaSpcyJcyFybxdgrjl bxdGrjl = new OaSpcyJcyFybxdgrjl();
        bxdGrjl.setId(GetUuIdUtils.ReplaceUuId());

        bxdGrjl.setFsqsxid(sqsxid);

        bxdGrjl.setFbxdid(fybxd.getId());
        bxdGrjl.setFuserid(user.getId());

        bxdGrjl.setRealname(user.getRealname());
        bxdGrjl.setSex(user.getSex());
        bxdGrjl.setFsfzh(user.getFsfzh());
        bxdGrjl.setPhone(user.getPhone());
        bxdGrjl.setFgzdw(user.getFgzdw());

        bxdGrjl.setFkfh(user.getFkfh());
        bxdGrjl.setFyhkh(user.getFyhkh());
        bxdGrjl.setFkhhszd(user.getFkhhszd());

        bxdGrjl.setFpjks(user.getOrgName());
        bxdGrjl.setFpjksid(user.getOrgId());
        bxdGrjl.setFpjr(user.getRealname());
        bxdGrjl.setFpjrid(user.getId());

        bxdGrjl.setFccts(String.valueOf(fybxd.getFjcts()));
        bxdGrjl.setFccbzbz(NkConstant.CCBZ_BZ);

        BigDecimal bzbz = new BigDecimal(NkConstant.CCBZ_BZ);
        BigDecimal ccts = new BigDecimal(fybxd.getFjcts());

        BigDecimal ccje = bzbz.multiply(ccts);

        bxdGrjl.setFccbzje(String.valueOf(ccje));

//        int zxfts = 0; // // 住宿费天数  默认按检查天数 -1
        BigDecimal zxfts = ccts.subtract(new BigDecimal(1));


        String lwfsbje = "0";
        String zxfybje = "0";

        bxdGrjl.setFlwfsbje(lwfsbje);

        bxdGrjl.setFcsjjtfs(OaJcyFybxdConstant.CSJJTFS_DEFAULT_GT); // 城市间交通方式  单位派车/无单位派车/公共交通 （默认公共交通，如果选其他不需要填后面的城市间交通费字段）

        bxdGrjl.setFwjtfsfdf(OaJcyFybxdConstant.FYBXD_SFDF_NO); // 城市间交通费-往-是否垫付 否/是（默认否，选是必填垫付人）
        bxdGrjl.setFwjtfdfrid(""); // 城市间交通费-往-垫付人id 从本次审评查验费用报销个人记录选择人员id
        bxdGrjl.setFwjtfqscs(""); // 城市间交通费-往-起始城市 可选全国的城市，前面14个显示广西地市
        bxdGrjl.setFwjtfddcs(""); // 城市间交通费-往-到达城市 可选全国的城市，前面14个显示广西地市
        bxdGrjl.setFwjtfsbje("0"); // 城市间交通费-往-实报金额 如果本人选择是垫付 实报金额为0，把本人的合计金额加到垫付人的实报金额

        bxdGrjl.setFfjtfsfdf(OaJcyFybxdConstant.FYBXD_SFDF_NO); // 城市间交通费-返-是否垫付 否/是（默认否，选是必填垫付人）
        bxdGrjl.setFfjtfdfrid(""); // 城市间交通费-返-垫付人id 从本次审评查验费用报销个人记录选择人员id
        bxdGrjl.setFfjtfqscs(""); // 城市间交通费-返-起始城市 可选全国的城市，前面14个显示广西地市
        bxdGrjl.setFfjtfddcs(""); // 城市间交通费-返-到达城市 可选全国的城市，前面14个显示广西地市
        bxdGrjl.setFfjtfsbje("0"); // 城市间交通费-返-实报金额 如果本人选择是垫付 实报金额为0，把本人的合计金额加到垫付人的实报金额

        bxdGrjl.setFzxfts(Integer.valueOf(String.valueOf(zxfts))); // 住宿费天数  默认按检查天数
        bxdGrjl.setFzxfsfdf(OaJcyFybxdConstant.FYBXD_SFDF_NO); // 住宿费是否垫付 否/是（默认否，选是必填垫付人）
        bxdGrjl.setFzxfdfrid(""); // 住宿费垫付人id 从本次审评查验费用报销个人记录选择人员id
        bxdGrjl.setFzxfsbje(zxfybje); // 住宿费实报金额 如果本人选择是垫付 实报金额为0，把本人的合计金额加到垫付人的实报金额


        bxdGrjl.setFsfzz(OaJcyFybxdConstant.FKFS_ZZ_NO);
        bxdGrjl.setFsfgwkfzsf(OaJcyFybxdConstant.FKFS_ZZ_NO);
        bxdGrjl.setFsfgwkdfclfw(OaJcyFybxdConstant.FKFS_ZZ_NO);
        bxdGrjl.setFsfgwkdfclff(OaJcyFybxdConstant.FKFS_ZZ_NO);
        /**
         * 差旅费应报合计金额 = 伙食补助实报金额+城市间交通费-往-合计金额+城市间交通费-返-合计金额+住宿费合计金额
         * 差旅费实报合计金额 = 伙食补助实报金额+城市间交通费-往-实报金额+城市间交通费-返-实报金额+住宿费实报金额
         */

        bxdGrjl.setFclfsbhjje("0");

        spcyJcyFybxdgrjlService.save(bxdGrjl);
    }


    private void updateFybxdGrjlByDg(String sqsxid,YcSysUser user, OaSpcyJcyFybxd fybxd){
        List<OaSpcyJcyFybxdgrjl> mxList = spcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, fybxd.getId())
                .eq(OaSpcyJcyFybxdgrjl::getFuserid, user.getId()));
        if (CollectionUtil.isNotEmpty(mxList)){
            String id = mxList.get(0).getId();
            OaSpcyJcyFybxdgrjl bxdGrjl = spcyJcyFybxdgrjlService.getById(id);
            //填报人信息
            bxdGrjl.setFuserid(user.getId());
            bxdGrjl.setRealname(fybxd.getFjbr());

            //收款人银行信息
            bxdGrjl.setFkfh(fybxd.getFkhyh());
            bxdGrjl.setFyhkh(fybxd.getFkhyhkh());
            bxdGrjl.setFkhhszd(fybxd.getFkhyhszd());

            //一般报销金额
            bxdGrjl.setFybbxje(fybxd.getFybbxje());

            //通过预算项目id 来判断是否是劳务费
            OaSpcyJcyFybxd oaSpcyJcyFybxd = spcyJcyFybxdService.getById(bxdGrjl.getFbxdid());
            String fysxmid = oaSpcyJcyFybxd.getFysxmid();
            if (StringUtils.isNotBlank(fysxmid)){
                NkRsXmyszbb rsXmyszbb = nkRsXmyszbbService.getById(fysxmid);
                if (rsXmyszbb.getFbmjjflmc().equals(OaJcyFybxdConstant.BXXMFL_LWF)){
                    //劳务费报销金额 是 一般报销金额
                    bxdGrjl.setFlwfsbje(fybxd.getFybbxje());

                    //劳务税个税
                    if(StringUtils.isNotBlank(fybxd.getFybbxje())){
                        Map getsqje = getsqje(fybxd);
                        bxdGrjl.setFlwfgsje(String.valueOf(getsqje.get("tax"))); //个税
                        bxdGrjl.setFlwfsqje(String.valueOf(getsqje.get("preTaxAmount"))); //税前金额
                    }
                }
            }


            spcyJcyFybxdgrjlService.updateById(bxdGrjl);
        }else {

            OaSpcyJcyFybxdgrjl bxdGrjl = new OaSpcyJcyFybxdgrjl();
            bxdGrjl.setId(GetUuIdUtils.ReplaceUuId());

            bxdGrjl.setFsqsxid(sqsxid);
            bxdGrjl.setFbxdid(fybxd.getId());

            //填报人信息
            bxdGrjl.setFuserid(user.getId());
            bxdGrjl.setFpjks(user.getOrgName());
            bxdGrjl.setFpjr(user.getRealname());
            bxdGrjl.setFpjksid(user.getOrgId());

            //
            bxdGrjl.setRealname(fybxd.getFjbr());

            //收款人银行信息
            bxdGrjl.setFkfh(fybxd.getFkhyh());
            bxdGrjl.setFyhkh(fybxd.getFkhyhkh());
            bxdGrjl.setFkhhszd(fybxd.getFkhyhszd());

            //一般报销金额
            bxdGrjl.setFybbxje(fybxd.getFybbxje());

            OaSpcyJcyFybxd oaSpcyJcyFybxd = spcyJcyFybxdService.getById(bxdGrjl.getFbxdid());
            String fysxmid = oaSpcyJcyFybxd.getFysxmid();
            if (StringUtils.isNotBlank(fysxmid)){
            NkRsXmyszbb rsXmyszbb = nkRsXmyszbbService.getById(fysxmid);
            if (rsXmyszbb.getFbmjjflmc().equals(OaJcyFybxdConstant.BXXMFL_LWF)){
                //劳务费报销金额 是 一般报销金额
                bxdGrjl.setFlwfsbje(fybxd.getFybbxje());

                //劳务税个税
                if(StringUtils.isNotBlank(fybxd.getFybbxje())){
                    Map getsqje = getsqje(fybxd);
                    bxdGrjl.setFlwfgsje(String.valueOf(getsqje.get("tax"))); //个税
                    bxdGrjl.setFlwfsqje(String.valueOf(getsqje.get("preTaxAmount"))); //税前金额
                }
            }
            }


            bxdGrjl.setFcsjjtfs(OaJcyFybxdConstant.CSJJTFS_DEFAULT_GT); // 城市间交通方式  单位派车/无单位派车/公共交通 （默认公共交通，如果选其他不需要填后面的城市间交通费字段）

            bxdGrjl.setFwjtfsfdf(OaJcyFybxdConstant.FYBXD_SFDF_NO); // 城市间交通费-往-是否垫付 否/是（默认否，选是必填垫付人）
            bxdGrjl.setFwjtfdfrid(""); // 城市间交通费-往-垫付人id 从本次审评查验费用报销个人记录选择人员id
            bxdGrjl.setFwjtfqscs(""); // 城市间交通费-往-起始城市 可选全国的城市，前面14个显示广西地市
            bxdGrjl.setFwjtfddcs(""); // 城市间交通费-往-到达城市 可选全国的城市，前面14个显示广西地市
            bxdGrjl.setFwjtfsbje("0"); // 城市间交通费-往-实报金额 如果本人选择是垫付 实报金额为0，把本人的合计金额加到垫付人的实报金额

            bxdGrjl.setFfjtfsfdf(OaJcyFybxdConstant.FYBXD_SFDF_NO); // 城市间交通费-返-是否垫付 否/是（默认否，选是必填垫付人）
            bxdGrjl.setFfjtfdfrid(""); // 城市间交通费-返-垫付人id 从本次审评查验费用报销个人记录选择人员id
            bxdGrjl.setFfjtfqscs(""); // 城市间交通费-返-起始城市 可选全国的城市，前面14个显示广西地市
            bxdGrjl.setFfjtfddcs(""); // 城市间交通费-返-到达城市 可选全国的城市，前面14个显示广西地市
            bxdGrjl.setFfjtfsbje("0"); // 城市间交通费-返-实报金额 如果本人选择是垫付 实报金额为0，把本人的合计金额加到垫付人的实报金额

            bxdGrjl.setFzxfsfdf(OaJcyFybxdConstant.FYBXD_SFDF_NO); // 住宿费是否垫付 否/是（默认否，选是必填垫付人）
            bxdGrjl.setFzxfdfrid(""); // 住宿费垫付人id 从本次审评查验费用报销个人记录选择人员id


            bxdGrjl.setFsfzz(OaJcyFybxdConstant.FKFS_ZZ_NO);
            bxdGrjl.setFsfgwkfzsf(OaJcyFybxdConstant.FKFS_ZZ_NO);
            bxdGrjl.setFsfgwkdfclfw(OaJcyFybxdConstant.FKFS_ZZ_NO);
            bxdGrjl.setFsfgwkdfclff(OaJcyFybxdConstant.FKFS_ZZ_NO);


            bxdGrjl.setFclfsbhjje("0");

            spcyJcyFybxdgrjlService.save(bxdGrjl);
        }
    }


    public Map getsqje(OaSpcyJcyFybxd fybxd){

        BigDecimal ybbxje = new BigDecimal(fybxd.getFybbxje());
       Map map = new HashMap();
        // 计算个税和税前金额
        BigDecimal tax = calculatePersonalIncomeTax(ybbxje);
        BigDecimal preTaxAmount = calculatePreTaxIncome(ybbxje, tax);
        map.put("tax",tax); //个税
        map.put("preTaxAmount",preTaxAmount); //税前金额
        return map;
    }

    /**
     * 根据实发金额计算个人所得税
     * @param actualAmount 实发劳务报酬金额
     * @return 个人所得税
     */
    public static BigDecimal calculatePersonalIncomeTax(BigDecimal actualAmount) {
        if (actualAmount.compareTo(BigDecimal.valueOf(800)) < 0) {
            return BigDecimal.ZERO;
        } else if (actualAmount.compareTo(BigDecimal.valueOf(4000)) <= 0) {
            //个税先算税前
            BigDecimal sq = (actualAmount.subtract(BigDecimal.valueOf(160))).divide(BigDecimal.valueOf(0.8));
            //个税
            BigDecimal multiply = (sq.subtract(BigDecimal.valueOf(800))).multiply(BigDecimal.valueOf(0.2));
            return multiply;
        } else {
            BigDecimal taxablePortion = actualAmount.multiply(BigDecimal.valueOf(0.8));
            return taxablePortion.multiply(BigDecimal.valueOf(0.2)); // 根据示例中的税率计算税额
        }
    }

    /**
     * 根据实发金额和已计算的个税，反推出税前金额（此处对超过4000元部分的税前金额做了简化处理）
     * @param actualAmount 实发劳务报酬金额
     * @param tax 已计算出的个人所得税
     * @return 税前金额
     */
    public static BigDecimal calculatePreTaxIncome(BigDecimal actualAmount, BigDecimal tax) {
        if (tax.compareTo(BigDecimal.ZERO) == 0) {
            return actualAmount;
        } else if (actualAmount.compareTo(BigDecimal.valueOf(4000)) <= 0) {
            BigDecimal add = actualAmount.add(tax);
            return add;
        } else {
            // 对于超过4000元的情况，此处只计算了实际应纳税所得额对应的税前金额
            // 实际上，完整的税前金额还需结合税率变化和速算扣除数等细节进行计算
            BigDecimal add = actualAmount.add(tax);
            return add;
        }
    }


    /**
     * 获取申请依据
     */
    public String getsqyjList(String sqsxid,String flcbm) {

        List nrList = new ArrayList();

        String fxm ="";
        String jh = "";
        List<String> fpid = Arrays.asList(sqsxid.split("[,，]"));

        if (flcbm.equals(OaFlowConstant.NK_CCSP)){
            for (String pid:fpid){
                List<NkRsCcsp> list = nkRsCcspService.list(new LambdaQueryWrapper<NkRsCcsp>().eq(NkRsCcsp::getId, pid));
                fxm = list.get(0).getFxm();
                jh = fxm+ "申请的出差申请";
                nrList.add(jh);
            }

        }else if (flcbm.equals(OaFlowConstant.OA_BGYPSQ)){
            for (String pid:fpid){
                List<OaBgypsqgl> list = oaBgypsqglService.list(new LambdaQueryWrapper<OaBgypsqgl>().eq(OaBgypsqgl::getId, pid));
                fxm = list.get(0).getFlyr();
                jh = fxm+ "申请的办公用品申领";
                nrList.add(jh);
            }

        }else if (flcbm.equals(OaFlowConstant.OA_XZSWSP)){
            for (String pid:fpid){
                List<OaXzswspEntity> list = oaxzswspService.list(new LambdaQueryWrapper<OaXzswspEntity>().eq(OaXzswspEntity::getId, pid));
                fxm = list.get(0).getFnbr();
                jh = fxm + "申请的事项申请";
                nrList.add(jh);
            }

        }else if (flcbm.equals(OaFlowConstant.NK_HTSPLC)) {
            for (String pid:fpid){
                List<NkRsHtspjl> list = nkRsHtspjlService.list(new LambdaQueryWrapper<NkRsHtspjl>().eq(NkRsHtspjl::getId, pid));
                fxm = list.get(0).getFtxr();
                jh = fxm + "申请报销的"+list.get(0).getFhtmc();
                nrList.add(jh);
            }
        }else if (flcbm.equals(OaFlowConstant.NK_JCXSWSQ)){
            for (String pid:fpid){
                List<NkJcxswsq> list = nkJcxswsqService.list(new LambdaQueryWrapper<NkJcxswsq>().eq(NkJcxswsq::getId, pid));
                fxm = list.get(0).getFsqr();
                jh = fxm + "申请经常性事务报销："+list.get(0).getFsqnr();
                nrList.add(jh);
            }
        }
        return nrList.toString();
    }


    /**
     * 获取内容描述
     */
    public String getFnrms(String sqsxid,String flcbm) {
        List<String> fpid = Arrays.asList(sqsxid.split("[,，]"));
        String nrms = "";
        if (flcbm.equals(OaFlowConstant.NK_CCSP)){
            List<NkRsCcsp> nkRsCcsps = nkRsCcspService.listByIds(fpid);
            nrms = nkRsCcsps.get(0).getFccsy();

        }else if(flcbm.equals(OaFlowConstant.OA_BGYPSQ)){
            List<OaBgypsqgl> oaBgypsqgls = oaBgypsqglService.listByIds(fpid);
            nrms = oaBgypsqgls.get(0).getFlyks() + "对" + oaBgypsqgls.get(0).getFpm() + "申请" + oaBgypsqgls.get(0).getFsl() + oaBgypsqgls.get(0).getFdw();

        }else if (flcbm.equals(OaFlowConstant.OA_XZSWSP)){
            List<OaXzswspEntity> oaXzswspEntities = oaxzswspService.listByIds(fpid);

            nrms = oaXzswspEntities.get(0).getFsx();

        }else if (flcbm.equals(OaFlowConstant.NK_HTSPLC)){
            List<NkRsHtspjl> oaNkRsHtsps  = nkRsHtspjlService.listByIds(fpid);

            nrms = oaNkRsHtsps.get(0).getFhtmc();

        }else if(flcbm.equals(OaFlowConstant.NK_JCXSWSQ)) {
            List<NkJcxswsq> nkJcxswsqs = nkJcxswsqService.listByIds(fpid);
            nrms = nkJcxswsqs.get(0).getFsqnr();
        }
        return nrms;

    }


    /**
     * 初始化费用报销单-检查员个人记录
     * @param
     * @param fybxd 费用报销单
     */
    private void initJcyFybxdGrjl(String sqsxid,String flcbm, OaSpcyJcyFybxd fybxd){
        List<String> idList = Arrays.asList(sqsxid.split(",|，"));
        //根据流程编码来生成对应的费用报销单
        if(flcbm.equals(OaFlowConstant.NK_CCSP)){
            List<NkRsCcsp> nkRsCcsps = nkRsCcspService.listByIds(idList);
            for (int order = 0; order < nkRsCcsps.size(); order++){
                YcSysUser user = sysUserService.getById(nkRsCcsps.get(order).getFuserid());
                if (user != null) {
                    //2024-5-6 ：出差审批同行人是否为空，如果同行人为空那就只有一个，如果不为空 那就循环同行人字段添加报销单个人记录
                    if (StringUtils.isBlank(nkRsCcsps.get(order).getFcctxryid())){
                        initFybxdGrjl(user, fybxd);
                    }else {
                        initFybxdGrjl(user, fybxd);
                        String[] cctxryids = nkRsCcsps.get(order).getFcctxryid().split(",");
                        for (int i = 0; i < cctxryids.length; i++){
                            YcSysUser users = sysUserService.getById(cctxryids[i]);
                            if (users != null) {
                                initFybxdGrjl(users, fybxd);
                            }
                        }
                    }


                }
            }
        }else if(flcbm.equals(OaFlowConstant.OA_BGYPSQ)){
            List<OaBgypsqgl> oaBgypsqgls = oaBgypsqglService.listByIds(idList);
            for (int order = 0; order < oaBgypsqgls.size(); order++){
                YcSysUser user = sysUserService.getById(oaBgypsqgls.get(order).getFuserid());
                if (user != null) {
                    initFybxdGrjl(user, fybxd);
                }
            }
        }else if(flcbm.equals(OaFlowConstant.OA_XZSWSP)){
            List<OaXzswspEntity> oaXzswspEntities = oaxzswspService.listByIds(idList);
            for (int order = 0; order < oaXzswspEntities.size(); order++){
                YcSysUser user = sysUserService.getById(oaXzswspEntities.get(order).getFuserid());
                if (user != null) {
                    initFybxdGrjl(user, fybxd);
                }
            }
        }else if (flcbm.equals(flcbm.equals(OaFlowConstant.NK_HTSPLC))){
            List<NkRsHtspjl> nkRsHtspjls = nkRsHtspjlService.listByIds(idList);
            for (int order = 0; order < nkRsHtspjls.size(); order++){
                YcSysUser user = sysUserService.getById(nkRsHtspjls.get(order).getFtxrid());
                if (user != null) {
                    initFybxdGrjl(user, fybxd);
                }
            }
        }else if (flcbm.equals(OaFlowConstant.NK_JCXSWSQ)){
            List<NkJcxswsq> nkJcxswsqs = nkJcxswsqService.listByIds(idList);
            for (int order = 0; order < nkJcxswsqs.size(); order++){
                YcSysUser user = sysUserService.getById(nkJcxswsqs.get(order).getFsqrid());
                if (user != null) {
                    initFybxdGrjl(user, fybxd);
                }
            }
        }

    }


    /**
     * 初始化费用报销单-新增人员个人记录
     * @param user 用户
     * @param fybxd 报销单
     *
     */
    private void initFybxdGrjl(YcSysUser user, OaSpcyJcyFybxd fybxd){
        List<OaSpcyJcyFybxdgrjl> mxList = spcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, fybxd.getId())
                .eq(OaSpcyJcyFybxdgrjl::getFuserid, user.getId()));
        if (CollectionUtil.isNotEmpty(mxList)){
            return;
        }

        OaSpcyJcyFybxdgrjl bxdGrjl = new OaSpcyJcyFybxdgrjl();
        bxdGrjl.setId(GetUuIdUtils.ReplaceUuId());
        bxdGrjl.setFsqsxid(fybxd.getFsqsxid());
        bxdGrjl.setFbxdid(fybxd.getId());
        bxdGrjl.setFuserid(user.getId());

        bxdGrjl.setRealname(user.getRealname());
        bxdGrjl.setSex(user.getSex());
        bxdGrjl.setFsfzh(user.getFsfzh());
        bxdGrjl.setPhone(user.getPhone());
        bxdGrjl.setFgzdw(user.getFgzdw());

        bxdGrjl.setFkfh(user.getFkfh());
        bxdGrjl.setFyhkh(user.getFyhkh());
        bxdGrjl.setFkhhszd(user.getFkhhszd());

        bxdGrjl.setFpjks(fybxd.getFjbks());
        bxdGrjl.setFpjksid(fybxd.getFjbksid());
        bxdGrjl.setFpjr(fybxd.getFjbr());
        bxdGrjl.setFpjrid(fybxd.getFjbrid());

        bxdGrjl.setFccts(String.valueOf(fybxd.getFjcts()));
        bxdGrjl.setFccbzbz(NkConstant.CCBZ_BZ);

        BigDecimal bzbz = new BigDecimal(NkConstant.CCBZ_BZ);
        BigDecimal ccts = new BigDecimal(fybxd.getFjcts());

        BigDecimal ccje = bzbz.multiply(ccts);

        bxdGrjl.setFccbzje(String.valueOf(ccje));


        bxdGrjl.setFsfzz(OaJcyFybxdConstant.FKFS_ZZ_NO);

//        int zxfts = 0; // // 住宿费天数  默认按检查天数 - 1

        BigDecimal zxfts = ccts.subtract(new BigDecimal(1));


        String zxfybje = "0";

        bxdGrjl.setFcsjjtfs(OaJcyFybxdConstant.CSJJTFS_DEFAULT_GT); // 城市间交通方式  单位派车/无单位派车/公共交通 （默认公共交通，如果选其他不需要填后面的城市间交通费字段）
        bxdGrjl.setFcsjjtfsf(OaJcyFybxdConstant.CSJJTFS_DEFAULT_GT);

        bxdGrjl.setFwjtfsfdf(OaJcyFybxdConstant.FYBXD_SFDF_NO); // 城市间交通费-往-是否垫付 否/是（默认否，选是必填垫付人）
        bxdGrjl.setFwjtfdfrid(""); // 城市间交通费-往-垫付人id 从本次审评查验费用报销个人记录选择人员id
        bxdGrjl.setFwjtfqscs(""); // 城市间交通费-往-起始城市 可选全国的城市，前面14个显示广西地市
        bxdGrjl.setFwjtfddcs(""); // 城市间交通费-往-到达城市 可选全国的城市，前面14个显示广西地市
        bxdGrjl.setFwjtfsbje("0"); // 城市间交通费-往-实报金额 如果本人选择是垫付 实报金额为0，把本人的合计金额加到垫付人的实报金额

//       if (StringUtils.isNotBlank(fybxd.getF))

        bxdGrjl.setFfjtfsfdf(OaJcyFybxdConstant.FYBXD_SFDF_NO); // 城市间交通费-返-是否垫付 否/是（默认否，选是必填垫付人）
        bxdGrjl.setFfjtfdfrid(""); // 城市间交通费-返-垫付人id 从本次审评查验费用报销个人记录选择人员id
        bxdGrjl.setFfjtfqscs(""); // 城市间交通费-返-起始城市 可选全国的城市，前面14个显示广西地市
        bxdGrjl.setFfjtfddcs(""); // 城市间交通费-返-到达城市 可选全国的城市，前面14个显示广西地市
        bxdGrjl.setFfjtfsbje("0"); // 城市间交通费-返-实报金额 如果本人选择是垫付 实报金额为0，把本人的合计金额加到垫付人的实报金额

        bxdGrjl.setFzxfts(Integer.valueOf(String.valueOf(zxfts))); // 住宿费天数  默认按检查天数
        bxdGrjl.setFzxfsfdf(OaJcyFybxdConstant.FYBXD_SFDF_NO); // 住宿费是否垫付 否/是（默认否，选是必填垫付人）
        bxdGrjl.setFzxfdfrid(""); // 住宿费垫付人id 从本次审评查验费用报销个人记录选择人员id
        bxdGrjl.setFzxfsbje(zxfybje); // 住宿费实报金额 如果本人选择是垫付 实报金额为0，把本人的合计金额加到垫付人的实报金额

        bxdGrjl.setFsfgwkdfclff(OaJcyFybxdConstant.FYBXD_SFDF_NO);
        bxdGrjl.setFsfgwkdfclfw(OaJcyFybxdConstant.FYBXD_SFDF_NO);
        bxdGrjl.setFsfgwkfzsf(OaJcyFybxdConstant.FYBXD_SFDF_NO);



        /**
         * 差旅费应报合计金额 = 伙食补助实报金额+城市间交通费-往-合计金额+城市间交通费-返-合计金额+住宿费合计金额
         * 差旅费实报合计金额 = 伙食补助实报金额+城市间交通费-往-实报金额+城市间交通费-返-实报金额+住宿费实报金额
         */

        bxdGrjl.setFclfsbhjje("0");

        spcyJcyFybxdgrjlService.save(bxdGrjl);
    }

    /**
     * 删除费用报销单及明细
     * @param fbxdid
     */
    public void deleteFybxdAndRymx(String fbxdid){
        if (StringUtils.isBlank(fbxdid)){
            throw new BusinessException("参数为空！");
        }
        OaSpcyJcyFybxd bean = spcyJcyFybxdService.getById(fbxdid);
        if (bean == null){
            throw new BusinessException("没有相匹配的数据！");
        }

        spcyJcyFybxdService.removeById(fbxdid);
        spcyJcyFybxdgrjlService.remove(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, fbxdid));

        String log = LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(bean));
        spcyLogService.addLogs(OaLogConstant.LOGS_BX_BXSQ, log, fbxdid, "删除检查员费用报销单及明细", OaLogConstant.LOGS_SYS_TYPE_SC);
    }

    /**
     * 查询费用报销单-分页列表
     */
    public HttpResult queryFybxdPageList(OaSpcyJcyFybxd fybxd, String gjz, boolean isSelectMy, PageVo pageVo, OrderItemVo orderItemVo){
        Page<OaSpcyJcyFybxd> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());

        if (isSelectMy){
            Page<OaSpcyJcyFybxd> pageList = oaSpcyJcyFybxdMapper.selectFybxdPageList(page, gjz, GetCurrentUserUtils.getCurrentUserId(), fybxd, orderItemVo);

            return HttpResult.ok(pageList);
        }else {
            LambdaQueryWrapper<OaSpcyJcyFybxd> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(StringUtils.isNotBlank(fybxd.getFjcsxmc()), OaSpcyJcyFybxd::getFjcsxmc, fybxd.getFjcsxmc());

            if (StringUtils.isNotBlank(gjz)) {
                queryWrapper
                        .and(f -> f.like(OaSpcyJcyFybxd::getFbxnrms, gjz)
                                .or()
                                .like(OaSpcyJcyFybxd::getFjbr, gjz)
                                .or()
                                .like(OaSpcyJcyFybxd::getFjbks, gjz)
                                .or()
                                .like(OaSpcyJcyFybxd::getFjcsxmc, gjz)
                                .or()
                                .like(OaSpcyJcyFybxd::getFjcqymc, gjz)
                                .or()
                                .like(OaSpcyJcyFybxd::getFqydz, gjz)
                        );
            }
            Page<OaSpcyJcyFybxd> pageList = spcyJcyFybxdService.page(page, queryWrapper);

            return HttpResult.ok(pageList);
        }
    }

    /**
     * 查询报销单列表-根据sqsxid
     */
    public List<OaSpcyJcyFybxd> queryBxdListBySqsxId(String sqsxid, String flcbm) throws Exception {
        if (StringUtils.isBlank(sqsxid)){
            throw new BusinessException("申请事项参数为空！");
        }

        List<OaSpcyJcyFybxd> bxdList = spcyJcyFybxdService.list(new LambdaQueryWrapper<OaSpcyJcyFybxd>()
                .eq(OaSpcyJcyFybxd::getFsqsxid, sqsxid));
        if (CollectionUtil.isEmpty(bxdList)) {
            initSqsxFybxd(sqsxid,flcbm,bxdList.get(0).getFbxlx(),bxdList.get(0).getFsfdgzz());
        }

        return spcyJcyFybxdService.list(new LambdaQueryWrapper<OaSpcyJcyFybxd>()
                .eq(OaSpcyJcyFybxd::getFsqsxid, sqsxid)
                .orderByAsc(OaSpcyJcyFybxd::getCreateTime));
    }

    /**
     * 查询报销人员信息列表-根据sqsxidqueryBxryxxListByBxdId
     * 在这里更新出差审批的报销状态
     * @param sqsxid
     */
    public Map queryBxryxxListBySqsxId(String sqsxid,String flcbm) throws Exception {
        Map resultMap = new HashMap();
        List<OaSpcyJcyFybxd> bxdList = new ArrayList();
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();

        OaSpcyJcyFybxd fybxd = null;

        if (StringUtils.isBlank(sqsxid)){
            List<OaSpcyJcyFybxd> list = spcyJcyFybxdService.list(new LambdaQueryWrapper<OaSpcyJcyFybxd>()
                    .eq(OaSpcyJcyFybxd::getFjbrid, currentUser.getId())
                    .eq(OaSpcyJcyFybxd::getFsffqlc, OaCommonFlowConstant.SFFQLC_NO)
                    .eq(OaSpcyJcyFybxd::getFlcbm, flcbm));
            if (CollectionUtils.isNotEmpty(list)){
                fybxd = list.get(0);
            }


        }else {

            bxdList = spcyJcyFybxdService.list(new LambdaQueryWrapper<OaSpcyJcyFybxd>()
                    .like(OaSpcyJcyFybxd::getFsqsxid, "," + sqsxid + ",")  // 包含目标ID的情况，使用LIKE操作符模糊匹配
                    .or()
                    .eq(OaSpcyJcyFybxd::getFsqsxid, sqsxid)
                    .eq(OaSpcyJcyFybxd::getFjbrid, currentUser.getId()));  // 或者就是这个ID本身（假设存在只存单个ID的情况）
            if (CollectionUtil.isEmpty(bxdList)) {
                //如果没有在这里做初始化
                if(flcbm.equals(OaFlowConstant.NK_CCSP)){
                    String bxlx = OaJcyFybxdConstant.STATE_CLBX;
                    String sfdgzz = OaJcyFybxdConstant.STATE_SFDGZZ_NO;
                    this.initSqsxFybxd(sqsxid, flcbm,bxlx,sfdgzz);
                }

                List<OaSpcyJcyFybxd> bxdList2 = spcyJcyFybxdService.list(new LambdaQueryWrapper<OaSpcyJcyFybxd>()
                        .eq(OaSpcyJcyFybxd::getFsqsxid, sqsxid));

                if (CollectionUtil.isNotEmpty(bxdList2)) {
                    fybxd = bxdList2.get(0);
                } else {
                    fybxd = null;
                }

            } else {
                fybxd = bxdList.get(0);
            }
        }
        List<String> list = Arrays.asList(sqsxid.split("[,,]"));
        List bxmxList = new ArrayList();
        for(String id : list){
            List<OaSpcyJcyFybxdgrjl>  bxmxLists = spcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                    .eq(OaSpcyJcyFybxdgrjl::getFsqsxid, id)
                    .orderByAsc(OaSpcyJcyFybxdgrjl::getCreateTime));

            bxmxList.addAll(bxmxLists);

        }
        if(CollectionUtils.isNotEmpty(bxdList)){
            String fysxmid = bxdList.get(0).getFysxmid();
            if (StringUtils.isNotBlank(fysxmid)){
                List<String> fysxmidList = Arrays.asList(fysxmid.split(",|，"));
                for (String id : fysxmidList){
                    NkRsXmyszbb byId = nkRsXmyszbbService.getById(id);
                    resultMap.put("ysxmList", byId);
                }
            }
        }
        resultMap.put("bxd", fybxd);
        resultMap.put("bxmxList", bxmxList);

        //更新出差的报销状态
        if (StringUtils.isNotBlank(flcbm)){
            if(flcbm.equals(OaFlowConstant.NK_CCSP)){
                NkRsCcsp ccsp = nkRsCcspService.getById(sqsxid);
                if (ccsp != null){
                    ccsp.setFbxzt(NkConstant.BXZT_BXZ);
                    nkRsCcspService.updateById(ccsp);
                }
            } else if (flcbm.equals(OaFlowConstant.OA_XZSWSP)) {
                OaXzswspEntity oaXzswspEntity =  oaxzswspService.getById(sqsxid);
                if (oaXzswspEntity != null){
                    oaXzswspEntity.setFbxzt(NkConstant.BXZT_BXZ);
                    oaxzswspService.updateById(oaXzswspEntity);
                }
            }else if (flcbm.equals(OaFlowConstant.NK_HTSPLC)) {
                NkRsHtspjl nkRsHtspjl = nkRsHtspjlService.getById(sqsxid);
                if (nkRsHtspjl != null) {
                    nkRsHtspjl.setFbxzt(NkConstant.BXZT_BXZ);
                    nkRsHtspjlService.updateById(nkRsHtspjl);
                }

            }else if (flcbm.equals(OaFlowConstant.NK_JCXSWSQ)){
                NkJcxswsq nkJcxswsq = nkJcxswsqService.getById(sqsxid);
                nkJcxswsq.setFsfysq(NkConstant.SQZT_WSQ);
                nkJcxswsqService.updateById(nkJcxswsq);
            }
        }
        return resultMap;
    }




    /**
     * 查询报销人员信息列表-根据sqsxidqueryBxryxxListByBxdId
     * 在这里更新出差审批的报销状态
     * @param
     */
    public Map queryBxryxxListByDjh(String djh,String flcbm,String sqsxid) throws Exception {
        Map resultMap = new HashMap();
        List<OaSpcyJcyFybxd> bxdList = new ArrayList();
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();

        OaSpcyJcyFybxd fybxd = null;

        if (StringUtils.isBlank(sqsxid)){
            List<OaSpcyJcyFybxd> list = spcyJcyFybxdService.list(new LambdaQueryWrapper<OaSpcyJcyFybxd>()
                    .eq(OaSpcyJcyFybxd::getFdjh, djh)
                    .eq(OaSpcyJcyFybxd::getFlcbm, flcbm));
            if (CollectionUtils.isNotEmpty(list)){
                fybxd = list.get(0);
            }


        }else {

            bxdList = spcyJcyFybxdService.list(new LambdaQueryWrapper<OaSpcyJcyFybxd>()
                    .eq(OaSpcyJcyFybxd::getFdjh, djh)
                    .eq(OaSpcyJcyFybxd::getFjbrid, currentUser.getId()));  // 或者就是这个ID本身（假设存在只存单个ID的情况）
            if (CollectionUtil.isEmpty(bxdList)) {
                List<OaSpcyJcyFybxd> bxdList2 = spcyJcyFybxdService.list(new LambdaQueryWrapper<OaSpcyJcyFybxd>()
                        .eq(OaSpcyJcyFybxd::getFdjh, djh));

                if (CollectionUtil.isNotEmpty(bxdList2)) {
                    fybxd = bxdList2.get(0);
                } else {
                    fybxd = null;
                }

            } else {
                fybxd = bxdList.get(0);
            }
        }
        List bxmxList = new ArrayList();
        if (fybxd != null){
            String id = fybxd.getId();
            List<OaSpcyJcyFybxdgrjl>  bxmxLists = spcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                    .eq(OaSpcyJcyFybxdgrjl::getFbxdid, id)
                    .orderByAsc(OaSpcyJcyFybxdgrjl::getCreateTime));

        bxmxList.addAll(bxmxLists);

        }
        if(CollectionUtils.isNotEmpty(bxdList)){
            String fysxmid = bxdList.get(0).getFysxmid();
            if (StringUtils.isNotBlank(fysxmid)){
                List<String> fysxmidList = Arrays.asList(fysxmid.split(",|，"));
                for (String id : fysxmidList){
                    NkRsXmyszbb byId = nkRsXmyszbbService.getById(id);
                    resultMap.put("ysxmList", byId);
                }
            }
        }
        resultMap.put("bxd", fybxd);
        resultMap.put("bxmxList", bxmxList);



        return resultMap;
    }



    /**
     * 查询报销人员信息列表-根据报销单id
     * @param bxdid
     */
    public List<OaSpcyJcyFybxdgrjl> queryBxryxxListByBxdId(String bxdid){
        List<OaSpcyJcyFybxdgrjl> list = new ArrayList<>();
        OaSpcyJcyFybxd oaSpcyJcyFybxd = spcyJcyFybxdService.getById(bxdid);
        String fbxdlx = oaSpcyJcyFybxd.getFbxdlx();
        if (fbxdlx.equals(OaJcyFybxdConstant.FYBXD_ZZRYCCBXD)){
            list = spcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                    .eq(OaSpcyJcyFybxdgrjl::getFbxdid, bxdid)
                    .orderByAsc(OaSpcyJcyFybxdgrjl::getCreateTime));
        }else {
            list = spcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                    .eq(OaSpcyJcyFybxdgrjl::getFbxdid, bxdid)
                    .orderByAsc(OaSpcyJcyFybxdgrjl::getCreateTime));
        }
        return list;
    }

    /**
     * 查询专家劳务费列表-根据报销单id
     * @param bxdid
     */
    public List<OaSpcyJcyFybxdgrjl> queryZjlwfList(String bxdid){
        List<OaSpcyJcyFybxdgrjl> list = spcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, bxdid)
                .eq(OaSpcyJcyFybxdgrjl::getFlwfsfjgs, OaJcyFybxdConstant.FYBXD_LWFSFJGS_YES)
                .orderByAsc(OaSpcyJcyFybxdgrjl::getCreateTime));

        return list;
    }

    /**
     * 查询当前记录的对应垫付人员列表
     * @param fbxdid 报销单id
     * @param id 报销人员记录id
     */
    public List<Map> queryFybxdDfryList(String fbxdid, String id){

        List<Map> resultList = new ArrayList<>();

        List<OaSpcyJcyFybxdgrjl> list = spcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, fbxdid)
                .ne(StringUtils.isNotBlank(id), OaSpcyJcyFybxdgrjl::getId, id));
        if (CollectionUtil.isNotEmpty(list)) {
            for (OaSpcyJcyFybxdgrjl b : list) {
                Map dataMap = new HashMap();
                dataMap.put("id", b.getFuserid());
                dataMap.put("realname", b.getRealname());
                dataMap.put("sex", b.getSex());
                dataMap.put("fgzdw", b.getFgzdw());
                dataMap.put("phone", b.getPhone());
                resultList.add(dataMap);
            }
        }

        return resultList;
    }

    /**
     * 新增报销人员用户信息
     */
    public YcSysUser addBxryUserInfo(AddJcyBxryInfoVo bean) throws InvocationTargetException, IllegalAccessException {
        if (StringUtils.isBlank(bean.getFbxdid())){
            throw new BusinessException("参数为空!");
        }

        OaSpcyJcyFybxd fybxd = spcyJcyFybxdService.getById(bean.getFbxdid());
        if (fybxd == null){
            throw new BusinessException("没有相匹配的数据！");
        }

        if (StringUtils.isBlank(bean.getRealname())){
            throw new BusinessException("姓名为空!");
        }

        if (StringUtils.isBlank(bean.getFsfzh())){
            throw new BusinessException("身份证号为空!");
        }

        if (StringUtils.isBlank(bean.getPhone())){
            throw new BusinessException("手机号为空!");
        }

        if (StringUtils.isBlank(bean.getFgzdw())){
            throw new BusinessException("工作单位为空!");
        }

        String realname = StringPatternUtils.trimString(bean.getRealname());
        String fsfzh = StringPatternUtils.trimString(bean.getFsfzh());
        String phone = StringPatternUtils.trimString(bean.getPhone());

        // 验证身份证号
        List<YcSysUser> sfzhUserList = sysUserService.selectUserBySfzh(fsfzh);
        if (CollectionUtil.isNotEmpty(sfzhUserList)){
            throw new BusinessException("身份证号已经存在！");
        }

        // 验证手机号
        List<YcSysUser> phoneUserList = sysUserService.selectUserBySfzh(fsfzh);
        if (CollectionUtil.isNotEmpty(phoneUserList)){
            throw new BusinessException("手机号已经存在！");
        }

        String id = GetUuIdUtils.ReplaceUuId();
        YcSysUser user = new YcSysUser();
        user.setId(id);
        BeanUtils.copyProperties(user, bean);
        user.setRealname(realname);
        user.setFsfzh(fsfzh);
        user.setPhone(phone);

        // 解析身份证号获取出生年月
        try {
            Date birthDayFromIdCard = DateConversionUtils.getBirthDayFromIdCard(fsfzh);
            if (birthDayFromIdCard != null){
                user.setBirthday(birthDayFromIdCard);
            }
        } catch (ParseException e) {
            log.error("身份证解析失败！");
        }

        // 获取性别
        int sex = DateConversionUtils.getSexFromIdCard(fsfzh);
        user.setSex(sex);

        user.setCreateTime(new Date());
        user.setDelFlag(SysUserConstant.DEL_FLAG_YES);
        user.setStatus(SysUserConstant.STATUS_YES);
        sysUserService.save(user);

        initFybxdGrjl(user, fybxd);

        String operContent = "删除：";
        operContent += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(user));

        spcyLogService.addLogs(OaLogConstant.LOGS_BX_BXSQ,operContent,bean.getSqsxid(),"新增费用报销单人员用户信息", OaLogConstant.LOGS_SYS_TYPE_XZ);

        return user;
    }

    /**
     * 查询可选择人员列表
     */
    public HttpResult queryKSelectUserPageList(YcSysUser query,String gjz, PageVo pageVo){
        return HttpResult.ok(sysUserService.getFybxdUserPageList(query,gjz,pageVo));
    }

    /**
     * 保存选择报销单-报销人员
     */
    public void saveSelectBxry(String ids, String fbxdid, String sqsxid){
        if (StringUtils.isBlank(ids)){
            throw new BusinessException("请选择人员为空！");
        }

        OaSpcyJcyFybxd fybxd = null;

        if (StringUtils.isNotBlank(fbxdid)) {
            fybxd = spcyJcyFybxdService.getById(fbxdid);
        }else {
            if (StringUtils.isBlank(sqsxid)){
                throw new BusinessException("参数为空！");
            }

            if (StringUtils.isNotBlank(sqsxid)) {
                List<OaSpcyJcyFybxd> list = spcyJcyFybxdService.list(new LambdaQueryWrapper<OaSpcyJcyFybxd>()
                        .eq(OaSpcyJcyFybxd::getFsqsxid, sqsxid));
                if (CollectionUtil.isEmpty(list)) {
                    throw new BusinessException("没有相匹配的数据！");
                }else {
                    fybxd = list.get(0);
                }
            }
        }

        if (fybxd == null) {
            throw new BusinessException("没有相匹配的数据！");
        }

        List<String> idsList = StringPatternUtils.getListByIds(ids);
        for (String userid : idsList) {
            YcSysUser user = sysUserService.getById(userid);
            if (user == null) {
                throw new BusinessException("没有相匹配的用户数据！");
            }

            initFybxdGrjl(user, fybxd);
        }
    }

    /**
     * 更新报销人员明细
     */
    public void updateBxd(HashMap<String,String> buffer) throws IllegalAccessException, InvocationTargetException {
        String id = buffer.get("id");
        if (StringUtils.isBlank(id)){
            throw new BusinessException("参数为空！");
        }

        OaSpcyJcyFybxd updateBean = spcyJcyFybxdService.getById(id);
        if (updateBean == null){
            throw new BusinessException("没有相匹配数据！");
        }

        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(updateBean);

        ConvertUtils.register(new DateConvert(), Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(updateBean, buffer);

        updateBean.setUpdateTime(new Date());
        updateBean.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
        if (StringUtils.isNotBlank(buffer.get("fybbxje"))){
            updateBean.setFhjbxjedx(NumberToChineseCapital.toChineseMoney(new BigDecimal(buffer.get("fybbxje"))));
        }

        spcyJcyFybxdService.updateById(updateBean);

        String operContent = "基本信息修改：";
        operContent += LogUtils.BeanModifyMsg(updateBean,oldMap);

        spcyLogService.addLogs(OaLogConstant.LOGS_BX_BXSQ,operContent,updateBean.getFsqsxid(),"修改费用报销单", OaLogConstant.LOGS_SYS_TYPE_XG);
    }

    /**
     * 更新报销人员明细
     */
    public void updateBxrymxxx(HashMap<String,String> buffer) throws IllegalAccessException, InvocationTargetException {
        String id = buffer.get("id");
        if (StringUtils.isBlank(id)){
            throw new BusinessException("参数为空！");
        }

        OaSpcyJcyFybxdgrjl updateBean = spcyJcyFybxdgrjlService.getById(id);
        if (updateBean == null){
            throw new BusinessException("没有相匹配数据！");
        }


        ConvertUtils.register(new DateConvert(), Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(updateBean, buffer);

        // 不为公共交通，相关字段全部置空
        if (StringUtils.isNotBlank(updateBean.getFcsjjtfs())
                && updateBean.getFcsjjtfs().equals(OaJcyFybxdConstant.CSJJTFS_DEFAULT_DWPC)){
            updateBean.setFwjtfsfdf(OaJcyFybxdConstant.FYBXD_SFDF_NO);
            updateBean.setFwjtfsbje("0");


            updateBean.setFwjtfdfrid("");
            updateBean.setFwjtfdfrxm("");
            updateBean.setFwjtfqscs("");
            updateBean.setFwjtfddcs("");

            updateBean.setFfjtfsfdf(OaJcyFybxdConstant.FYBXD_SFDF_NO);
            updateBean.setFfjtfsbje("0");


            updateBean.setFfjtfdfrid("");
            updateBean.setFfjtfdfrxm("");
            updateBean.setFfjtfqscs("");
            updateBean.setFfjtfddcs("");
        }

        if (StringUtils.isNotBlank(updateBean.getFfjtfsfdf()) && updateBean.getFfjtfsfdf().equals("否")){
            updateBean.setFfjtfdfrid("");
            updateBean.setFfjtfdfrxm("");
        }

        if (StringUtils.isNotBlank(updateBean.getFwjtfsfdf()) && updateBean.getFwjtfsfdf().equals("否")){
            updateBean.setFwjtfdfrid("");
            updateBean.setFwjtfdfrxm("");
        }

        if (StringUtils.isNotBlank(updateBean.getFzxfsfdf()) && updateBean.getFzxfsfdf().equals("否")){
            updateBean.setFzxfdfrid("");
            updateBean.setFzxfdfrxm("");
        }


        updateBean.setUpdateTime(new Date());
        updateBean.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
        spcyJcyFybxdgrjlService.updateById(updateBean);


        OaSpcyJcyFybxdgrjl byId = spcyJcyFybxdgrjlService.getById(id);

//        String operContent = "基本信息修改：";
//        operContent += LogUtils.BeanModifyMsg(updateBean,oldMap);

//        spcyLogService.addLogs(LogConstant.LOGS_JCY_FYBXD,operContent,updateBean.getFsqsxid(),"修改费用报销单明细", SpcyConstant.LOGS_SYS_TYPE_XG);
    }

    /**
     * 删除报销人员明细-根据报销人员id
     */
    public void deleteBxrymxById(String fybxdgrjlId){
        if (StringUtils.isBlank(fybxdgrjlId)){
            throw new BusinessException("参数为空！");
        }

        OaSpcyJcyFybxdgrjl updateBean = spcyJcyFybxdgrjlService.getById(fybxdgrjlId);
        if (updateBean == null){
            throw new BusinessException("没有相匹配数据！");
        }

        spcyJcyFybxdgrjlService.removeById(fybxdgrjlId);

        String operContent = "删除：";
        operContent += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(updateBean));

        spcyLogService.addLogs(OaLogConstant.LOGS_BX_BXSQ,operContent,updateBean.getFsqsxid(),"删除费用报销单", OaLogConstant.LOGS_SYS_TYPE_SC);

    }

    /**
     * 报销单信息-查询报销单
     */
    public OaSpcyJcyFybxd queryBxdById(String fbxdid){
        return spcyJcyFybxdService.getById(fbxdid);
    }

    /**
     * 报销单信息-提交
     */
    public HttpResult submitFybxd(String fbxdid) throws InvocationTargetException, IllegalAccessException {
        if (StringUtils.isBlank(fbxdid)){
            throw new BusinessException("参数为空！");
        }
        OaSpcyJcyFybxd bean = spcyJcyFybxdService.getById(fbxdid);
        if (bean == null){
            throw new BusinessException("没有相匹配的数据！");
        }

        String nextState = OaJcyFybxdConstant.getNextState(bean.getFbxdzt());
        if (nextState.equals(OaJcyFybxdConstant.STATE_TSSX)){
            if (StringUtils.isBlank(bean.getFsqsxid())){
                throw new BusinessException("请选择对应申请事项！");
            }


            List<OaSpcyJcyFybxdgrjl> jcyFybxdgrjls = spcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                    .eq(OaSpcyJcyFybxdgrjl::getFbxdid, fbxdid));
            if (CollectionUtils.isNotEmpty(jcyFybxdgrjls)){
                for (OaSpcyJcyFybxdgrjl jcyFybxdgrjl : jcyFybxdgrjls){
                    String fsqsxid = jcyFybxdgrjl.getFsqsxid();

                    List<OaSpcyJcyFybxdtssxsq> oaSpcyJcyFybxdtssxsqs = spcyJcyFybxdtssxsqService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdtssxsq>()
                            .eq(OaSpcyJcyFybxdtssxsq::getFsxsqid, fsqsxid)
                            .eq(OaSpcyJcyFybxdtssxsq::getFlczt, OaFlowConstants.HJMC_LCJS));

                    if (CollectionUtils.isNotEmpty(oaSpcyJcyFybxdtssxsqs)){
                        updatetssqbxdid(fbxdid,fsqsxid);

                    }
                }
            }
            //如果下一个环节是出差补助及其他。则判断是否选择预算项目
        } else if (nextState.equals(OaJcyFybxdConstant.STATE_CCBZJQT)){
            OaSpcyJcyFybxd oaSpcyJcyFybxd = spcyJcyFybxdService.getById(fbxdid);
            if (oaSpcyJcyFybxd != null){
                String fysxmid = oaSpcyJcyFybxd.getFysxmid();
                if (StringUtils.isBlank(fysxmid)){
                    throw new BusinessException("预算项目为空，请选择预算项目");
                }
            }

      //如果下一个环节是交通费，则需要处理 出差补助和其他金额的计算
      } else if (nextState.equals(OaJcyFybxdConstant.STATE_JTF)) {

            nkBxJtxcdService.initxcd(fbxdid);

        } else if (nextState.equals(OaJcyFybxdConstant.STATE_ZSF)){


            syncBxryZxlx(fbxdid); // 同步报销人员-住宿类型

            bean = syncFybxdInYwdwpc(bean, fbxdid); // 同步费用报销单-是否-有无单位派车

        }else if (nextState.equals(OaJcyFybxdConstant.STATE_HZBD)){

            updategrje(fbxdid);
//            updatejczt(fbxdid);
            gethzdByBxd(fbxdid);
            bean = getHzBxd(bean, fbxdid); // 计算费用

        }else if (nextState.equals(OaJcyFybxdConstant.STATE_WC)){

            OaSpcyJcyFybxd oaSpcyJcyFybxd = spcyJcyFybxdService.getById(fbxdid);
            if (StringUtils.isBlank(oaSpcyJcyFybxd.getFbxdj())){
                throw new BusinessException("单据数量不能为空，请编辑单据数量");
            }

            bean = getHzBxd(bean, fbxdid); // 计算费用

            initGenFybxxmmxJl(bean, fbxdid); // 生成报销明细

        }

        bean.setFbxdzt(nextState);
        spcyJcyFybxdService.updateById(bean);
        return HttpResult.ok("提交成功");
    }


    /**
     * 报销单信息-提交/对公或者对私
     */
    public HttpResult submitFybxds(String fbxdid) throws InvocationTargetException, IllegalAccessException {
        if (StringUtils.isBlank(fbxdid)){
            throw new BusinessException("参数为空！");
        }
        OaSpcyJcyFybxd bean = spcyJcyFybxdService.getById(fbxdid);
        CurrentUserVo currentUser = ThreadContextHelper.getCurrentUser();
        YcSysUser sysUser = ycSysUserService.getById(currentUser.getId());
        if (bean == null){
            throw new BusinessException("没有相匹配的数据！");
        }

        String nextState = OaJcyFybxdConstant.getNextStates(bean.getFbxdzt());
        if (nextState.equals(OaJcyFybxdConstant.STATE_YKJHSQ)){

            if (StringUtils.isBlank(bean.getFsqsxid())){
                throw new BusinessException("请选择对应申请事项！");
            }


            this.updateFybxdGrjlByDg(bean.getFsqsxid(),sysUser,bean);

        }else if (nextState.equals(OaJcyFybxdConstant.STATE_HZBD)){

          OaSpcyJcyFybxd oaSpcyJcyFybxd = spcyJcyFybxdService.getById(fbxdid);
          if (oaSpcyJcyFybxd != null) {
              String fysxmid = oaSpcyJcyFybxd.getFysxmid();
              if (StringUtils.isBlank(fysxmid)) {
                  throw new BusinessException("预算项目为空，请选择预算项目");
              }
          }

        }else if (nextState.equals(OaJcyFybxdConstant.STATE_WC)){
//            bean = getHzBxd(bean, fbxdid); // 计算费用
            this.updateFybxdGrjlByDg(bean.getFsqsxid(),sysUser,bean);
            initGenFybxxmmxJl(bean, fbxdid); // 生成报销明细

        }

        bean.setFbxdzt(nextState);
        spcyJcyFybxdService.updateById(bean);
        return HttpResult.ok("提交成功");
    }



    /**
     * 更新出差补助及其他 金额
     */
    public void updateCcbzjqt(String bxdid){
        List<OaSpcyJcyFybxdgrjl> jcyFybxdgrjls = spcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, bxdid)
                .isNull(OaSpcyJcyFybxdgrjl:: getFfkfs));
        BigDecimal ccbzje = new BigDecimal(0);
        BigDecimal zsfje = new BigDecimal(0);

        if (CollectionUtils.isNotEmpty(jcyFybxdgrjls)){
            for (OaSpcyJcyFybxdgrjl oaSpcyJcyFybxdgrjl : jcyFybxdgrjls){
                if (StringUtils.isNotBlank(oaSpcyJcyFybxdgrjl.getFccbzbz()) && StringUtils.isNotBlank(oaSpcyJcyFybxdgrjl.getFccts())){
                    BigDecimal ccbzbz = new BigDecimal(oaSpcyJcyFybxdgrjl.getFccbzbz()); //出差补助标准

                    BigDecimal ccts = new BigDecimal(oaSpcyJcyFybxdgrjl.getFccts()); //出差天数

                    ccbzje = ccts.multiply(ccbzbz);
                    oaSpcyJcyFybxdgrjl.setFccbzje(String.valueOf(ccbzje));
                    spcyJcyFybxdgrjlService.updateById(oaSpcyJcyFybxdgrjl);

                }
                if(StringUtils.isNotBlank(String.valueOf(oaSpcyJcyFybxdgrjl.getFzxfts())) &&  StringUtils.isNotBlank(oaSpcyJcyFybxdgrjl.getFzsfbz())){
                    BigDecimal zxfts = new BigDecimal(oaSpcyJcyFybxdgrjl.getFzxfts()); //住宿天数
                    BigDecimal zsfbz = new BigDecimal(oaSpcyJcyFybxdgrjl.getFzsfbz()); //住宿标准
                    zsfje = zxfts.multiply(zsfbz);
                    oaSpcyJcyFybxdgrjl.setFzxfsbje(String.valueOf(zsfje));
                    spcyJcyFybxdgrjlService.updateById(oaSpcyJcyFybxdgrjl);
                }
            }
        }
    }

    /**
     * 报销单信息-退回
     */
    public HttpResult returnFybxd(String fbxdid){
        if (StringUtils.isBlank(fbxdid)){
            throw new BusinessException("参数为空！");
        }

        OaSpcyJcyFybxd bean = spcyJcyFybxdService.getById(fbxdid);
        if (bean == null){
            throw new BusinessException("没有相匹配的数据！");
        }

        bean.setFbxdzt(OaJcyFybxdConstant.getPreviousState(bean.getFbxdzt()));
        spcyJcyFybxdService.updateById(bean);

        return HttpResult.ok("返回成功");
    }
    /**
     * 报销单信息-退回/对公或者对私
     */

    public HttpResult returnFybxds(String fbxdid){
        if (StringUtils.isBlank(fbxdid)){
            throw new BusinessException("参数为空！");
        }

        OaSpcyJcyFybxd bean = spcyJcyFybxdService.getById(fbxdid);
        if (bean == null){
            throw new BusinessException("没有相匹配的数据！");
        }

        bean.setFbxdzt(OaJcyFybxdConstant.getPreviousStates(bean.getFbxdzt()));
        spcyJcyFybxdService.updateById(bean);

        return HttpResult.ok("返回成功");
    }

    /**
     * 批量设置出行方式
     */
    public void batchSetBxdCxfsByBxdId(String fbxdid, String fcsjjtfs){
        if (StringUtils.isBlank(fbxdid)){
            throw new BusinessException("参数为空！");
        }
        if (StringUtils.isBlank(fcsjjtfs)){
            throw new BusinessException("出行方式为空！");
        }
        List<OaSpcyJcyFybxdgrjl> list = spcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, fbxdid));

        if (CollectionUtil.isNotEmpty(list)) {
            for (OaSpcyJcyFybxdgrjl grjl : list) {
                grjl.setFcsjjtfs(fcsjjtfs);

                if (fcsjjtfs.equals(OaJcyFybxdConstant.CSJJTFS_DEFAULT_DWPC)) {
                    grjl.setFfjtfdfrid("");
                    grjl.setFfjtfdfrxm("");
                    grjl.setFfjtfddcs("");
                    grjl.setFfjtfqscs("");
                    grjl.setFfjtfsbje("0");

                    grjl.setFfjtfsfdf(OaJcyFybxdConstant.FYBXD_SFDF_NO);

                    grjl.setFwjtfdfrid("");
                    grjl.setFwjtfdfrxm("");
                    grjl.setFwjtfddcs("");
                    grjl.setFwjtfqscs("");
                    grjl.setFwjtfsbje("0");

                    grjl.setFwjtfsfdf(OaJcyFybxdConstant.FYBXD_SFDF_NO);
                }
                if (fcsjjtfs.equals(OaJcyFybxdConstant.CXFS_FJ)){
                    grjl.setFsfgwkdfclff(OaJcyFybxdConstant.DFCF_NO);
                    grjl.setFsfgwkdfclfw(OaJcyFybxdConstant.DFCF_NO);
                }

                spcyJcyFybxdgrjlService.updateById(grjl);
            }
        }

    }

    /**
     * 批量设置劳务费/伙食/交通补助天数
     * @param fbxdid 报销单id
     * @param fjcts 天数
     */
    public void batchSzLwHsJtbzTs(String fbxdid, String fjcts){
        if (StringUtils.isBlank(fbxdid)){
            throw new BusinessException("参数为空！");
        }
        if (StringUtils.isBlank(fjcts)){
            throw new BusinessException("天数为空！");
        }
        Integer jcts = Integer.valueOf(fjcts);
        List<OaSpcyJcyFybxdgrjl> list = spcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, fbxdid));

        if (CollectionUtil.isNotEmpty(list)) {
            for (OaSpcyJcyFybxdgrjl grjl : list) {
                grjl.setFccts(String.valueOf(jcts));

                spcyJcyFybxdgrjlService.updateById(grjl);
            }
        }
    }

    /**
     * 同步报销人员-住宿类型
     */
    private void syncBxryZxlx(String fbxdid){
        List<OaSpcyJcyFybxdgrjl> list = spcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, fbxdid));

        if (CollectionUtil.isNotEmpty(list)) {
            for (OaSpcyJcyFybxdgrjl grjl : list) {
                // 是否存在特殊事项申请
                List<OaSpcyJcyFybxdtssxsq> tssxsqList = spcyJcyFybxdtssxsqService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdtssxsq>()
                        .eq(OaSpcyJcyFybxdtssxsq::getFsqlx, OaJcyFybxdConstant.FYBXD_TSSXSQ_SQLX_WXF)
                        .like(OaSpcyJcyFybxdtssxsq::getFwxfsqrid, grjl.getFuserid()));
                if (CollectionUtil.isNotEmpty(tssxsqList)){
                    grjl.setFzxlx(OaJcyFybxdConstant.FYBXD_BXRY_ZXLX_WX);
                }else {
                    grjl.setFzxlx(OaJcyFybxdConstant.FYBXD_BXRY_ZXLX_QT);
                }
                spcyJcyFybxdgrjlService.updateById(grjl);
            }
        }
    }

    /**
     * 同步费用报销单-是否-有无单位派车
     */
    private OaSpcyJcyFybxd syncFybxdInYwdwpc(OaSpcyJcyFybxd fybxd, String fbxdid){
        StringBuilder qzrid = new StringBuilder();
        StringBuilder qzrxm = new StringBuilder();
        String type = OaJcyFybxdConstant.FYBXD_SFYWDWPC_NO;

        List<OaSpcyJcyFybxdgrjl> list = spcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, fbxdid)
                .eq(OaSpcyJcyFybxdgrjl::getFcsjjtfs, OaJcyFybxdConstant.CSJJTFS_DEFAULT_WDWPC));
        if (CollectionUtil.isNotEmpty(list)){
            type = OaJcyFybxdConstant.FYBXD_SFYWDWPC_YES;

            for (OaSpcyJcyFybxdgrjl grjl : list){
                if (!StringPatternUtils.verifryIsEmptyStringBuilder(qzrid)){
                    qzrid.append(",");
                    qzrxm.append(",");
                }
                qzrid.append(grjl.getFuserid());
                qzrxm.append(grjl.getRealname());
            }
        }

        fybxd.setFsfywdwpc(type);
        fybxd.setFwcsjtzssmqzrid(qzrid.toString());
        fybxd.setFwcsjtzssmqzrxm(qzrxm.toString());

        return fybxd;
    }

    /**
     * 验证报销人员-是否填写签名
     */
    private void verifyIsExistsFybxdBxryInUserQm(String fbxdid){
        List<OaSpcyJcyFybxdgrjl> list = spcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, fbxdid));

        if (CollectionUtil.isNotEmpty(list)){
            for (OaSpcyJcyFybxdgrjl grjl : list) {
                if (StringUtils.isNotBlank(grjl.getFuserid())) {
                    SpcySqsxWjjl signWjjl = spcySqsxWjjlService.downMyUserQm(grjl.getFuserid(), null);
                    if (signWjjl == null){
                        throw new BusinessException("【"+grjl.getRealname()+"】没有上传相关签名附件！");
                    }
                }
            }
        }else {
            throw new BusinessException("报销人员列表为空！");
        }
    }



    /**
     * 生成汇总表
     * @param fybxd
     * @param fbxdid
     * @return
     */
    private OaSpcyJcyFybxd getHzBxd(OaSpcyJcyFybxd fybxd, String fbxdid){

        String fclfbxje = "0";

        List<OaSpcyJcyFybxdgrjl> bxmxList = queryBxryxxListByBxdId(fbxdid);
        if (CollectionUtil.isNotEmpty(bxmxList)){
            for (OaSpcyJcyFybxdgrjl bxmx : bxmxList){
//                if (bxmx.getFsfzz().equals(JcyFybxdConstant.FKFS_ZZ_NO)) {

                    /**
                     * // 城市间交通费-往-实报金额
                     * 如果本人选择是垫付 实报金额为0，把本人的合计金额加到垫付人的实报金额
                     */
//                    if (StringUtils.isNotBlank(bxmx.getFwjtfsfdf()) && bxmx.getFwjtfsfdf().equals("是")) {
//                        bxmx.setFwjtfsbje("0");
//                    } else {
//                        bxmx.setFwjtfsbje(bxmx.getFwjtfsbje());
//                    }


                    /**
                     *   城市间交通费-返-实报金额
                     *   如果本人选择是垫付 实报金额为0，把本人的合计金额加到垫付人的实报金额
                     */
//                    if (StringUtils.isNotBlank(bxmx.getFfjtfsfdf()) && bxmx.getFfjtfsfdf().equals("是")) {
//                        bxmx.setFfjtfsbje("0");
//                    } else {
//                        bxmx.setFfjtfsbje(bxmx.getFfjtfsbje());
//                    }


                    /**
                     * 查询垫付-城市间交通费-往-垫付人id
                     */
//                    List<SpcyJcyFybxdgrjl> wjtfList = spcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<SpcyJcyFybxdgrjl>()
//                            .eq(SpcyJcyFybxdgrjl::getFwjtfdfrid, bxmx.getFuserid())
//                            .eq(SpcyJcyFybxdgrjl::getFwjtfsfdf, "是"));
//                    BigDecimal wjtfTotal = new BigDecimal(bxmx.getFwjtfsbje());
//                    for (SpcyJcyFybxdgrjl w : wjtfList) {
//                        wjtfTotal = wjtfTotal.add(new BigDecimal(Optional.ofNullable(w.getFwjtfsbje()).orElseGet(() -> "0")));
//                        bxmx.setFwjtfsbje(wjtfTotal.toString());
//                    }

                    /**
                     * 城市间交通费-返-垫付人id
                     */
//                    List<SpcyJcyFybxdgrjl> fjtfList = spcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<SpcyJcyFybxdgrjl>()
//                            .eq(SpcyJcyFybxdgrjl::getFfjtfdfrid, bxmx.getFuserid())
//                            .eq(SpcyJcyFybxdgrjl::getFfjtfsfdf, "是"));
//                    BigDecimal fjtfTotal = new BigDecimal(bxmx.getFfjtfsbje());
//                    for (SpcyJcyFybxdgrjl f : fjtfList) {
//                        fjtfTotal = fjtfTotal.add(new BigDecimal(Optional.ofNullable(f.getFfjtfsbje()).orElseGet(() -> "0")));
//                        bxmx.setFfjtfsbje(String.valueOf(fjtfTotal));
//                    }
//                } else {
//                    bxmx.setFwjtfsbje(bxmx.getFwjtfsbje());
//                    bxmx.setFfjtfsbje(bxmx.getFfjtfsbje());
//                }



                    /**
                     *  住宿费实报金额
                     * 如果本人选择是垫付 实报金额为0，把本人的合计金额加到垫付人的实报金额
                     */
                    if (StringUtils.isNotBlank(bxmx.getFzxfsfdf()) && bxmx.getFzxfsfdf().equals("是")) {
                        bxmx.setFzxfsbje("0");
                    } else {
                        bxmx.setFzxfsbje(bxmx.getFzxfsbje());
                    }

                    /**
                     * 住宿费垫付人id
                     */
                    List<OaSpcyJcyFybxdgrjl> zxfList = spcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                            .eq(OaSpcyJcyFybxdgrjl::getFzxfdfrid, bxmx.getFuserid())
                            .eq(OaSpcyJcyFybxdgrjl::getFzxfsfdf, "是"));
                    BigDecimal zxfTotal = new BigDecimal(bxmx.getFzxfsbje());
                    for (OaSpcyJcyFybxdgrjl z : zxfList) {
                        zxfTotal = zxfTotal.add(new BigDecimal(Optional.ofNullable(z.getFzxfsbje()).orElseGet(() -> "0")));
                        bxmx.setFzxfsbje(String.valueOf(zxfTotal));
                    }

                    // 差旅费实报合计金额
                BigDecimal clfSbje = new BigDecimal("0");

                clfSbje = clfSbje.add(new BigDecimal(Optional.ofNullable(bxmx.getFccbzje()).orElseGet(() -> "0"))); //出差补助金额


                //其他金额在这里加，这里要循环相加，这里要循环相加摘要表的报销单数据，或者在这之前将其他金额表的数据加上去
                clfSbje = clfSbje.add(new BigDecimal(Optional.ofNullable(bxmx.getFqtje()).orElseGet(() -> "0"))); //其他金额

                List<NkBxJtxcd> nkBxJtxcds = nkBxJtxcdService.list(new LambdaQueryWrapper<NkBxJtxcd>()
                        .eq(NkBxJtxcd::getFbxdgrid, bxmx.getId()));
                for (NkBxJtxcd nkBxJtxcd:nkBxJtxcds){
                    clfSbje = clfSbje.add(new BigDecimal(nkBxJtxcd.getFwjtfybje()));
                }



//                clfSbje = clfSbje.add(new BigDecimal(bxmx.getFfjtfsbje()));
                clfSbje = clfSbje.add(new BigDecimal(bxmx.getFzxfsbje()));
                bxmx.setFclfsbhjje(String.valueOf(clfSbje));
                fclfbxje = String.valueOf(new BigDecimal(fclfbxje).add(new BigDecimal(bxmx.getFclfsbhjje())));
                spcyJcyFybxdgrjlService.updateById(bxmx);
            }
        }

        fybxd.setFclfbxje(fclfbxje); // 差旅费报销金额
//        String tssxsqBxje = jsFybxdTssxsqBxJe(fbxdid); // 特殊事项申请报销金额，报销金额已经加了，所以在此处不需要加

        BigDecimal bxdTotal = new BigDecimal("0");  // 合计报销金额
        bxdTotal = bxdTotal.add(new BigDecimal(fclfbxje));
//        bxdTotal = bxdTotal.add(new BigDecimal(tssxsqBxje));


        fybxd.setFhjbxje(String.valueOf(bxdTotal));
        fybxd.setFhjbxjedx(NumberToChineseCapital.toChineseMoney(bxdTotal));
        spcyJcyFybxdService.updateById(fybxd);
        return fybxd;
    }

    /**
     * 计算特殊事项申请报销金额
     * @return
     */
    private String jsFybxdTssxsqBxJe(String fbxdid){
        BigDecimal total = new BigDecimal("0");

        List<OaSpcyJcyFybxdtssxsq> list = spcyJcyFybxdtssxsqService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdtssxsq>()
                .eq(OaSpcyJcyFybxdtssxsq::getFbxdid, fbxdid));
        if (CollectionUtil.isNotEmpty(list)){
            for (OaSpcyJcyFybxdtssxsq sq : list){
                total = total.add(new BigDecimal(Optional.ofNullable(sq.getFsqje()).orElseGet(()->"0")));
            }
        }

        return total.toString();
    }




    /**
     * 初始化生成-费用报销项目明细记录
     */
    private void initGenFybxxmmxJl(OaSpcyJcyFybxd fybxd, String fbxdid) throws InvocationTargetException, IllegalAccessException {
        //如果已经生成 过 先删除
        List<OaSpcyJcyFybxxmmxjl> oaSpcyJcyFybxxmmxjls1 = spcyJcyFybxxmmxkaslService.list(new LambdaQueryWrapper<OaSpcyJcyFybxxmmxjl>()
                .eq(OaSpcyJcyFybxxmmxjl::getFbxdid, fbxdid));
        if (CollectionUtil.isNotEmpty(oaSpcyJcyFybxxmmxjls1)){
            spcyJcyFybxxmmxkaslService.removeBatchByIds(oaSpcyJcyFybxxmmxjls1);

        }


        if (StringUtils.isBlank(fbxdid)){
            return;
        }
        // 报销单
        if (fybxd == null) {
            fybxd = spcyJcyFybxdService.getById(fbxdid);
            if (fybxd == null) {
                return;
            }
        }
        // 报销单人员记录
        List<OaSpcyJcyFybxdgrjl> bxgrjlList = spcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, fbxdid));

        if (CollectionUtil.isEmpty(bxgrjlList)){
            return;
        }

        List<OaSpcyJcyFybxxmmxjl> oaSpcyJcyFybxxmmxjls = spcyJcyFybxxmmxkaslService.list(new LambdaQueryWrapper<OaSpcyJcyFybxxmmxjl>().eq(OaSpcyJcyFybxxmmxjl::getFbxdid, fbxdid));

        if (CollectionUtil.isEmpty(oaSpcyJcyFybxxmmxjls)){


            // 生成报销项目明细
            for (OaSpcyJcyFybxdgrjl grjl : bxgrjlList){
                String userid = grjl.getFuserid();
                YcSysUser sysUser = sysUserService.getById(userid);

                this.syncUserYkhOrGwk(grjl);

                //公务卡
                String fgwkbxje = grjl.getFgwkbxje();
                //转账
                String fzzbxje = grjl.getFzzbxje();
                //储蓄卡
                String fcxkbxje = grjl.getFcxkbxje();
                //一般报销金额
                String fybbxje = grjl.getFybbxje();

                //报销单类型
                OaSpcyJcyFybxd fybxd1 = spcyJcyFybxdService.getById(fbxdid);


                if (StringUtils.isNotBlank(fgwkbxje) && !"0".equals(fgwkbxje)){
                    OaSpcyJcyFybxxmmxjl oaSpcyJcyFybxxmmxjl = new OaSpcyJcyFybxxmmxjl();
                    BeanUtils.copyProperties(oaSpcyJcyFybxxmmxjl,fybxd);
                    oaSpcyJcyFybxxmmxjl.setId(GetUuIdUtils.ReplaceUuId());
                    oaSpcyJcyFybxxmmxjl.setFbxxmfl1(OaJcyFybxdConstant.BXXMFL_CLF);
                    oaSpcyJcyFybxxmmxjl.setFbxdid(fybxd.getId());
                    oaSpcyJcyFybxxmmxjl.setFbxdgrjlid(grjl.getId());
                    oaSpcyJcyFybxxmmxjl.setFsqsxid(grjl.getFsqsxid());
                    oaSpcyJcyFybxxmmxjl.setFuserid(grjl.getFuserid());
                    oaSpcyJcyFybxxmmxjl.setFyhkh(sysUser.getFgwkh()); //卡号
                    oaSpcyJcyFybxxmmxjl.setFkhh(sysUser.getFgwkkhh()); //开户行
                    oaSpcyJcyFybxxmmxjl.setFkhhszd(sysUser.getFgwkszd()); //开户行所在地
                    oaSpcyJcyFybxxmmxjl.setFbzmx(grjl.getFgwkbxje()); //报账明细
                    oaSpcyJcyFybxxmmxjl.setFkl(OaJcyFybxdConstant.KL_GWK ); //卡类
                    oaSpcyJcyFybxxmmxjl.setRealname(sysUser.getRealname());
                    oaSpcyJcyFybxxmmxjl.setFpjks(sysUser.getOrgName());
                    spcyJcyFybxxmmxkaslService.save(oaSpcyJcyFybxxmmxjl);
                }
                if (StringUtils.isNotBlank(fcxkbxje) && !"0".equals(fcxkbxje)){
                    OaSpcyJcyFybxxmmxjl oaSpcyJcyFybxxmmxjl = new OaSpcyJcyFybxxmmxjl();
                    BeanUtils.copyProperties(oaSpcyJcyFybxxmmxjl,fybxd);
                    oaSpcyJcyFybxxmmxjl.setId(GetUuIdUtils.ReplaceUuId());
                    oaSpcyJcyFybxxmmxjl.setFbxxmfl1(OaJcyFybxdConstant.BXXMFL_CLF);
                    oaSpcyJcyFybxxmmxjl.setFbxdid(fybxd.getId());
                    oaSpcyJcyFybxxmmxjl.setFbxdgrjlid(grjl.getId());
                    oaSpcyJcyFybxxmmxjl.setFsqsxid(grjl.getFsqsxid());
                    oaSpcyJcyFybxxmmxjl.setFuserid(grjl.getFuserid());
                    oaSpcyJcyFybxxmmxjl.setFyhkh(sysUser.getFyhkh()); //卡号
                    oaSpcyJcyFybxxmmxjl.setFkhh(sysUser.getFkfh()); //开户行
                    oaSpcyJcyFybxxmmxjl.setFkhhszd(sysUser.getFkhhszd()); //开户行所在地
                    oaSpcyJcyFybxxmmxjl.setFbzmx(grjl.getFcxkbxje()); //报账明细
                    oaSpcyJcyFybxxmmxjl.setFkl(OaJcyFybxdConstant.KL_CXK); //卡类
                    oaSpcyJcyFybxxmmxjl.setRealname(sysUser.getRealname());
                    oaSpcyJcyFybxxmmxjl.setFpjks(sysUser.getOrgName());
                    spcyJcyFybxxmmxkaslService.save(oaSpcyJcyFybxxmmxjl);
                }

                if (StringUtils.isNotBlank(fzzbxje) && !"0".equals(fzzbxje)){
                    List<NkRsZzsqd> rsZzsqds = nkRsZzzsqdService.list(new LambdaQueryWrapper<NkRsZzsqd>()
                            .eq(NkRsZzsqd::getFbxdid, grjl.getFbxdid())
                            .eq(NkRsZzsqd::getFsqrid, grjl.getFuserid()));

                    BigDecimal NYHKFWYXGWbigDecimal = new BigDecimal(0);
                    BigDecimal FFSWFWYXGSbigDecimal = new BigDecimal(0);

                    for (NkRsZzsqd rsZzsqd:rsZzsqds){
                        if (rsZzsqd.getFskdw().equals(OaSpcyJcyFybxdConstant.FJ_SKDW_NYHKFWYXGW)){
                            NYHKFWYXGWbigDecimal = NYHKFWYXGWbigDecimal.add(new BigDecimal(rsZzsqd.getFjexx()));
                        }
                        if (rsZzsqd.getFskdw().equals(OaSpcyJcyFybxdConstant.FJ_SKDW_FFSWFWYXGS)){
                            FFSWFWYXGSbigDecimal = FFSWFWYXGSbigDecimal.add(new BigDecimal(rsZzsqd.getFjexx()));
                        }
                    }


                    if (NYHKFWYXGWbigDecimal.compareTo(new BigDecimal(0)) > 0){
                        List<NkDgzzgslb> nkDgzzgslbs = ndgzzgslbService.list(new LambdaQueryWrapper<NkDgzzgslb>()
                                .eq(NkDgzzgslb::getFmc, OaSpcyJcyFybxdConstant.FJ_SKDW_NYHKFWYXGW));

                        OaSpcyJcyFybxxmmxjl oaSpcyJcyFybxxmmxjl1 = new OaSpcyJcyFybxxmmxjl();

                        BeanUtils.copyProperties(oaSpcyJcyFybxxmmxjl1,fybxd);
                        oaSpcyJcyFybxxmmxjl1.setId(GetUuIdUtils.ReplaceUuId());
                        oaSpcyJcyFybxxmmxjl1.setFbxxmfl1(OaJcyFybxdConstant.BXXMFL_CLF);
                        oaSpcyJcyFybxxmmxjl1.setFbxdid(fybxd.getId());
                        oaSpcyJcyFybxxmmxjl1.setFbxdgrjlid(grjl.getId());
                        oaSpcyJcyFybxxmmxjl1.setFsqsxid(grjl.getFsqsxid());
                        oaSpcyJcyFybxxmmxjl1.setFuserid(grjl.getFuserid());
                        oaSpcyJcyFybxxmmxjl1.setRealname(sysUser.getRealname());
                        oaSpcyJcyFybxxmmxjl1.setFpjks(sysUser.getOrgName());

                        oaSpcyJcyFybxxmmxjl1.setFyhkh(nkDgzzgslbs.get(0).getFzh()); //卡号
                        oaSpcyJcyFybxxmmxjl1.setFkhh(nkDgzzgslbs.get(0).getFkhh() +"("+nkDgzzgslbs.get(0).getFmc()+")"); //开户行

                        oaSpcyJcyFybxxmmxjl1.setFbzmx(String.valueOf(NYHKFWYXGWbigDecimal)); //报账明细
                        oaSpcyJcyFybxxmmxjl1.setFkl(OaJcyFybxdConstant.KL_ZZ); //卡类
                        spcyJcyFybxxmmxkaslService.save(oaSpcyJcyFybxxmmxjl1);
                    }
                    if (FFSWFWYXGSbigDecimal.compareTo(new BigDecimal(0)) > 0) {
                        List<NkDgzzgslb> nkDgzzgslbs = ndgzzgslbService.list(new LambdaQueryWrapper<NkDgzzgslb>()
                                .eq(NkDgzzgslb::getFmc, OaSpcyJcyFybxdConstant.FJ_SKDW_FFSWFWYXGS));
                            OaSpcyJcyFybxxmmxjl oaSpcyJcyFybxxmmxjl = new OaSpcyJcyFybxxmmxjl();
                            BeanUtils.copyProperties(oaSpcyJcyFybxxmmxjl,fybxd);
                            oaSpcyJcyFybxxmmxjl.setId(GetUuIdUtils.ReplaceUuId());
                            oaSpcyJcyFybxxmmxjl.setFbxxmfl1(OaJcyFybxdConstant.BXXMFL_CLF);
                            oaSpcyJcyFybxxmmxjl.setFbxdid(fybxd.getId());
                            oaSpcyJcyFybxxmmxjl.setFbxdgrjlid(grjl.getId());
                            oaSpcyJcyFybxxmmxjl.setFsqsxid(grjl.getFsqsxid());
                            oaSpcyJcyFybxxmmxjl.setFuserid(grjl.getFuserid());
                            oaSpcyJcyFybxxmmxjl.setRealname(sysUser.getRealname());
                            oaSpcyJcyFybxxmmxjl.setFpjks(sysUser.getOrgName());
                            oaSpcyJcyFybxxmmxjl.setFyhkh(nkDgzzgslbs.get(0).getFzh()); //卡号
                            oaSpcyJcyFybxxmmxjl.setFkhh(nkDgzzgslbs.get(0).getFkhh() +"("+nkDgzzgslbs.get(0).getFmc()+")"); //开户行

                            oaSpcyJcyFybxxmmxjl.setFbzmx(String.valueOf(FFSWFWYXGSbigDecimal)); //报账明细
                            oaSpcyJcyFybxxmmxjl.setFkl(OaJcyFybxdConstant.KL_ZZ); //卡类
                            spcyJcyFybxxmmxkaslService.save(oaSpcyJcyFybxxmmxjl);

                    }
                }

                if (StringUtils.isNotBlank(fybbxje) && !"0".equals(fybbxje)){

                    OaSpcyJcyFybxxmmxjl oaSpcyJcyFybxxmmxjl = new OaSpcyJcyFybxxmmxjl();
                    BeanUtils.copyProperties(oaSpcyJcyFybxxmmxjl,fybxd);
                    oaSpcyJcyFybxxmmxjl.setId(GetUuIdUtils.ReplaceUuId());
                    //报销明细分类使用预算指标的部门经济分类
                    OaSpcyJcyFybxd oaSpcyJcyFybxd = spcyJcyFybxdService.getById(grjl.getFbxdid());
                    NkRsXmyszbb rsXmyszbb = nkRsXmyszbbService.getById(oaSpcyJcyFybxd.getFysxmid());
                    oaSpcyJcyFybxxmmxjl.setFbxxmfl1(rsXmyszbb.getFbmjjflmc());

                    oaSpcyJcyFybxxmmxjl.setFbxdid(fybxd.getId());
                    oaSpcyJcyFybxxmmxjl.setFbxdgrjlid(grjl.getId());
                    oaSpcyJcyFybxxmmxjl.setFsqsxid(grjl.getFsqsxid());

                    //收款人信息
                    //姓名
                    oaSpcyJcyFybxxmmxjl.setRealname(grjl.getRealname());
                    //证件类型
                    oaSpcyJcyFybxxmmxjl.setFzjlx(grjl.getFzjlx());
                    //证件号码
                    oaSpcyJcyFybxxmmxjl.setFsfzh(grjl.getFsfzh());
                    oaSpcyJcyFybxxmmxjl.setFyhkh(grjl.getFyhkh()); //卡号
                    oaSpcyJcyFybxxmmxjl.setFkhh(grjl.getFkfh()); //开户行
                    oaSpcyJcyFybxxmmxjl.setFkhhszd(grjl.getFkhhszd()); //开户行所在地


                    if(fybxd1.getFbxdlx().equals(OaJcyFybxdConstant.FYBXD_DGZZSQD)){
                        oaSpcyJcyFybxxmmxjl.setFkl(OaJcyFybxdConstant.KL_DG); //卡类
                        oaSpcyJcyFybxxmmxjl.setFbzmx(grjl.getFybbxje()); //报账明细
                    }else {
                        oaSpcyJcyFybxxmmxjl.setFkl(OaJcyFybxdConstant.KL_DS); //卡类
                        oaSpcyJcyFybxxmmxjl.setFbzmx(grjl.getFlwfsqje()); //报账明细
                    }
                    //填报人信息
                    oaSpcyJcyFybxxmmxjl.setFpjrid(grjl.getFpjrid());
                    oaSpcyJcyFybxxmmxjl.setFpjr(grjl.getFpjr());
                    oaSpcyJcyFybxxmmxjl.setFpjks(grjl.getFpjks());
                    spcyJcyFybxxmmxkaslService.save(oaSpcyJcyFybxxmmxjl);

                }
            }

        }




    }

    private void syncUserYkhOrGwk(OaSpcyJcyFybxdgrjl grjl){
        if (grjl == null){
            return;
        }
        if (StringUtils.isBlank(grjl.getFuserid())){
            return;
        }
        boolean flag = false;
        LambdaUpdateWrapper<YcSysUser> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.eq(YcSysUser::getId, grjl.getFuserid());

        YcSysUser updateUser = new YcSysUser();

        // 银行卡
        if (StringUtils.isNotBlank(grjl.getFyhkh())){
            updateUser.setFyhkh(grjl.getFyhkh());
            flag = true;
        }
        if (StringUtils.isNotBlank(grjl.getFkfh())){
            updateUser.setFkfh(grjl.getFkfh());
            flag = true;
        }
        if (StringUtils.isNotBlank(grjl.getFkhhszd())){
            updateUser.setFkhhszd(grjl.getFkhhszd());
            flag = true;
        }

        // 公务卡
        if (StringUtils.isNotBlank(grjl.getFgwkh())){
            updateUser.setFgwkh(grjl.getFgwkh());
            flag = true;
        }
        if (StringUtils.isNotBlank(grjl.getFgwkkhh())){
            updateUser.setFgwkkhh(grjl.getFgwkkhh());
            flag = true;
        }
        if (StringUtils.isNotBlank(grjl.getFgwkszd())){
            updateUser.setFgwkszd(grjl.getFgwkszd());
            flag = true;
        }

        if (flag) {
            sysUserService.update(updateUser, updateWrapper);
        }

    }


    /**
     * 费用报销项目明细记录-分页查询
     */
    public HttpResult queryFybxxmmxPageList(OaSpcyJcyFybxxmmxjl query, String gjz, PageVo pageVo, OrderItemVo orderItemVo){

        Page<OaSpcyJcyFybxxmmxjl> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());

        LambdaQueryWrapper<OaSpcyJcyFybxxmmxjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(query.getFbxnrms()), OaSpcyJcyFybxxmmxjl::getFbxnrms, query.getFbxnrms());
        queryWrapper.like(StringUtils.isNotBlank(query.getFjcqymc()), OaSpcyJcyFybxxmmxjl::getFjcqymc, query.getFjcqymc());
        queryWrapper.like(StringUtils.isNotBlank(query.getFjcsxmc()), OaSpcyJcyFybxxmmxjl::getFjcsxmc, query.getFjcsxmc());
        queryWrapper.like(StringUtils.isNotBlank(query.getFgzdw()), OaSpcyJcyFybxxmmxjl::getFgzdw, query.getFgzdw());
        queryWrapper.like(StringUtils.isNotBlank(query.getFbxxmfl1()), OaSpcyJcyFybxxmmxjl::getFbxxmfl1, query.getFbxxmfl1());

        if (StringUtils.isNotBlank(gjz)){
            int selectSex = 3;
            if (gjz.contains("男")){
                selectSex = 1;
            }else if (gjz.contains("女")){
                selectSex = 2;
            }
            Integer finalSelectSex = selectSex;

            queryWrapper
                    .and(f->f.like(OaSpcyJcyFybxxmmxjl::getFuserid, gjz)
                            .or()
                            .like(OaSpcyJcyFybxxmmxjl::getFbxnrms, gjz)
                            .or()
                            .like(OaSpcyJcyFybxxmmxjl::getFjcqymc, gjz)
                            .or()
                            .like(OaSpcyJcyFybxxmmxjl::getFjcsxmc, gjz)
                            .or()
                            .like(OaSpcyJcyFybxxmmxjl::getFbxxmfl1, gjz)
                            .or()
                            .like(OaSpcyJcyFybxxmmxjl::getFgzdw, gjz)
                            .or()
                            .eq(OaSpcyJcyFybxxmmxjl::getSex, finalSelectSex)
                    );
        }

        Page<OaSpcyJcyFybxxmmxjl> pageList = spcyJcyFybxxmmxkaslService.page(page, queryWrapper);

        return HttpResult.ok(pageList);
    }

    /**
     * 特殊事项申请-分页查询
     */
    public HttpResult queryFybxdTssxSqPageList(String fbxdid, PageVo pageVo){
        if (StringUtils.isBlank(fbxdid)){
            fbxdid = "0";
        }
//        Page<SpcyJcyFybxdtssxsq> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());

        LambdaQueryWrapper<OaSpcyJcyFybxdtssxsq> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OaSpcyJcyFybxdtssxsq::getFbxdid, fbxdid);
//        lambdaQueryWrapper.eq(SpcyJcyFybxdtssxsq :: getFlczt,FlowConstant.HJMC_LCJS);

        lambdaQueryWrapper.orderByAsc(OaSpcyJcyFybxdtssxsq::getFsqrq);
        lambdaQueryWrapper.orderByAsc(OaSpcyJcyFybxdtssxsq::getCreateTime);
        List<OaSpcyJcyFybxdtssxsq> pageList = spcyJcyFybxdtssxsqService.list(lambdaQueryWrapper);

        return HttpResultVo.HttpResultList(pageList);
    }


    public HttpResult queryFybxdTssxSqPageListBysqsxid(String fsqsxid, PageVo pageVo){
        if (StringUtils.isBlank(fsqsxid)){
            fsqsxid = null;
        }
//        Page<SpcyJcyFybxdtssxsq> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());

        LambdaQueryWrapper<OaSpcyJcyFybxdtssxsq> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OaSpcyJcyFybxdtssxsq::getFsxsqid, fsqsxid);
        lambdaQueryWrapper.eq(OaSpcyJcyFybxdtssxsq::getFsqzt, OaCommonFlowConstant.FSFSQ_NO);

//        lambdaQueryWrapper.eq(SpcyJcyFybxdtssxsq :: getFlczt,FlowConstant.HJMC_LCJS);

        lambdaQueryWrapper.orderByAsc(OaSpcyJcyFybxdtssxsq::getFsqrq);
        lambdaQueryWrapper.orderByAsc(OaSpcyJcyFybxdtssxsq::getCreateTime);
        List<OaSpcyJcyFybxdtssxsq> pageList = spcyJcyFybxdtssxsqService.list(lambdaQueryWrapper);

        return HttpResultVo.HttpResultList(pageList);
    }


    public Result querytssxsqByfybxd(String fsqsxid, PageVo pageVo){
        if (StringUtils.isBlank(fsqsxid)){
            return  Result.error("申请事项id为空");
        }

        LambdaQueryWrapper<OaSpcyJcyFybxdtssxsq> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OaSpcyJcyFybxdtssxsq::getFsxsqid, fsqsxid);
        lambdaQueryWrapper.eq(OaSpcyJcyFybxdtssxsq::getFsqzt, "已申请");
        lambdaQueryWrapper.eq(OaSpcyJcyFybxdtssxsq:: getFlczt, OaFlowConstants.HJMC_LCJS);

        lambdaQueryWrapper.orderByAsc(OaSpcyJcyFybxdtssxsq::getFsqrq);
        lambdaQueryWrapper.orderByAsc(OaSpcyJcyFybxdtssxsq::getCreateTime);
        List<OaSpcyJcyFybxdtssxsq> pageList = spcyJcyFybxdtssxsqService.list(lambdaQueryWrapper);

        return Result.ok(pageList);
    }


    /**
     * 特殊事项申请-根据id查看
     */
    public OaSpcyJcyFybxdtssxsq queryFybxdTssxSqById(String id){
        if (StringUtils.isBlank(id)){
            throw new BusinessException("参数为空！");
        }
        return spcyJcyFybxdtssxsqService.getById(id);
    }




    /**
     * 新增-特殊事项申请
     */
    public OaSpcyJcyFybxdtssxsq addFybxdTssxSq(OaSpcyJcyFybxdtssxsq bean){
        CurrentUserVo ycuser = ThreadContextHelper.getCurrentUser();
        if (StringUtils.isBlank(bean.getFsxsqid())){
            throw new BusinessException("没有绑定对应的事项申请id！");
        }

        if (StringUtils.isBlank(bean.getFsqlx())){
            throw new BusinessException("申请类型为空！");
        }

        if (bean.getFsqts() == null){
            bean.setFsqts(0);
        }

        OaSpcyJcyFybxd bxd = spcyJcyFybxdService.getById(bean.getFbxdid());
//        if (bxd == null){
//            throw new BusinessException("没有相匹配的数据！");
//        }

        // 额外费用
        if (StringUtils.isNotBlank(bean.getFewfybxsqrid())){
            List<YcSysUser> userList = sysUserService.listByIds(StringPatternUtils.getListByIds(bean.getFewfybxsqrid()));
            String sqr = "";
            if (CollectionUtil.isNotEmpty(userList)){
                for (YcSysUser user : userList){
                    if (StringUtils.isNotBlank(sqr)){
                        sqr += "," + user.getRealname();
                    }else {
                        sqr += user.getRealname();
                    }
                }
            }
            bean.setFewfybxsqrxm(sqr);
        }

        // 午休
        if (StringUtils.isNotBlank(bean.getFwxfsqrid())){
            List<YcSysUser> userList = sysUserService.listByIds(StringPatternUtils.getListByIds(bean.getFwxfsqrid()));
            String sqr = "";
            if (CollectionUtil.isNotEmpty(userList)){
                for (YcSysUser user : userList){
                    if (StringUtils.isNotBlank(sqr)){
                        sqr += "," + user.getRealname();
                    }else {
                        sqr += user.getRealname();
                    }
                }
            }
            bean.setFwxfsqrxm(sqr);
        }

        bean.setId(GetUuIdUtils.ReplaceUuId());

        if (StringUtils.isBlank(bean.getFsqrq())){
            bean.setFsqrq(DateConversionUtils.DateToStringYYYYMMDD(new Date()));
        }

        // 经办科室
        bean.setFsqrid(ycuser.getId());
        bean.setFsqr(ycuser.getName());
        bean.setFsqks(ycuser.getOrgName());

//        if (StringUtils.isNotBlank(bxd.getFsqsxid())) {
//
//        }


        bean.setFxmnr(bxd.getFbxnrms());
        if (StringUtils.isBlank(bean.getFxmsj())) {
            bean.setFxmsj(bxd.getFbxsjms());
        }

//        bean.setFlczt(CommonFlowConstant.FLOW_LCZB);

        if (StringUtils.isBlank(bean.getFsqje())){
            bean.setFsqje("0");
        }
        spcyJcyFybxdtssxsqService.save(bean);

        // 报销特殊申请状态
        updateBxdTssxsq(bean.getFbxdid());

        String log = LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(bean));
        spcyLogService.addLogs(OaLogConstant.LOGS_BX_BXSQ,log,bean.getFbxdid(),"添加特殊事项申请", OaLogConstant.LOGS_SYS_TYPE_XZ);

        return bean;
    }


    /**
     * 初始化-特殊事项申请
     */
    public Result initFybxdTssxSq(String sqsxid,String fsqlx,String lcbm) throws Exception {
        CurrentUserVo ycuser = ThreadContextHelper.getCurrentUser();
        if (StringUtils.isBlank(sqsxid)){
            throw new BusinessException("没有绑定对应的事项申请id！");
        }
        OaSpcyJcyFybxdtssxsq fybxdtssxsq = new OaSpcyJcyFybxdtssxsq();

        if (StringUtils.isBlank(fsqlx)){
            throw new BusinessException("申请类型为空！");
        }
        fybxdtssxsq.setFsqlx(fsqlx);


        fybxdtssxsq.setId(GetUuIdUtils.ReplaceUuId());

        if (StringUtils.isBlank(fybxdtssxsq.getFsqrq())){
            fybxdtssxsq.setFsqrq(DateConversionUtils.DateToStringYYYYMMDD(new Date()));
        }

        // 申请人科室
        fybxdtssxsq.setFsqrid(ycuser.getId());
        fybxdtssxsq.setFsqr(ycuser.getName());
        fybxdtssxsq.setFsqks(ycuser.getOrgName());
        fybxdtssxsq.setFsxsqid(sqsxid);
        String sjms = getsjms(sqsxid, lcbm);
        String nrms = getnrms(sqsxid, lcbm);

        fybxdtssxsq.setFxmnr(nrms);
        fybxdtssxsq.setFxmsj(sjms);
        fybxdtssxsq.setFsqzt("未申请");
        fybxdtssxsq.setFsqje("0");
        fybxdtssxsq.setFlcbm(lcbm);
        spcyJcyFybxdtssxsqService.save(fybxdtssxsq);
        // 报销特殊申请状态
        String log = LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(fybxdtssxsq));
        spcyLogService.addLogs(OaLogConstant.LOGS_BX_BXSQ,log,fybxdtssxsq.getFbxdid(),"添加特殊事项申请", OaLogConstant.LOGS_SYS_TYPE_XZ);

        return Result.ok(fybxdtssxsq);
    }

    public String getnrms(String sqsxid,String lcbm) throws Exception {
        String bxnrms = "";

        if (lcbm.equals(OaFlowConstant.NK_CCSP)){
            NkRsCcsp ccsp = nkRsCcspService.getById(sqsxid);
            bxnrms = ccsp.getFccsy();
            //天数
            String fccksrq = ccsp.getFccksrq();
            String fccjsrq = ccsp.getFccjsrq();

        }

        return bxnrms;
    }

    public String getsjms(String sqsxid,String lcbm) throws Exception {
        //检查天数/天数
        int jcts = 0;

        String bxsjms = "";

        if (lcbm.equals(OaFlowConstant.NK_CCSP)){
            NkRsCcsp ccsp = nkRsCcspService.getById(sqsxid);

            //天数
            String fccksrq = ccsp.getFccksrq();
            String fccjsrq = ccsp.getFccjsrq();
            long day = DateConversionUtils.dateDiffWithDay(fccksrq, fccjsrq);
            jcts = (int) day;
            //报销时间描述
            bxsjms = fccksrq + "至" + fccjsrq + "共" + jcts + "天";
        }

        return bxsjms;
    }


    /**
     * 更新-特殊事项申请
     */
    public void updateFybxdTssxSq(HashMap<String,String> buffer) throws IllegalAccessException, InvocationTargetException {
        String id = buffer.get("id");
        if (StringUtils.isBlank(id)){
            throw new BusinessException("参数为空！");
        }

        OaSpcyJcyFybxdtssxsq updateBean = spcyJcyFybxdtssxsqService.getById(id);
        if (updateBean == null){
            throw new BusinessException("没有相匹配数据！");
        }

        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(updateBean);

        ConvertUtils.register(new DateConvert(), Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(updateBean, buffer);

        // 额外费用
        if (StringUtils.isNotBlank(updateBean.getFewfybxsqrid())){
            List<YcSysUser> userList = sysUserService.listByIds(StringPatternUtils.getListByIds(updateBean.getFewfybxsqrid()));
            String sqr = "";
            if (CollectionUtil.isNotEmpty(userList)){
                for (YcSysUser user : userList){
                    if (StringUtils.isNotBlank(sqr)){
                        sqr += "," + user.getRealname();
                    }else {
                        sqr += user.getRealname();
                    }
                }
            }
            updateBean.setFewfybxsqrxm(sqr);
        }

        // 午休
        if (StringUtils.isNotBlank(updateBean.getFwxfsqrid())){
            List<YcSysUser> userList = sysUserService.listByIds(StringPatternUtils.getListByIds(updateBean.getFwxfsqrid()));
            String sqr = "";
            if (CollectionUtil.isNotEmpty(userList)){
                for (YcSysUser user : userList){
                    if (StringUtils.isNotBlank(sqr)){
                        sqr += "," + user.getRealname();
                    }else {
                        sqr += user.getRealname();
                    }
                }
            }
            updateBean.setFwxfsqrxm(sqr);
        }

        updateBean.setUpdateTime(new Date());
        updateBean.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
        spcyJcyFybxdtssxsqService.updateById(updateBean);

        String operContent = "基本信息修改：";
        operContent += LogUtils.BeanModifyMsg(updateBean,oldMap);

        spcyLogService.addLogs(OaLogConstant.LOGS_BX_BXSQ,operContent,updateBean.getFbxdid(),"修改特殊事项申请", OaLogConstant.LOGS_SYS_TYPE_XG);
    }

    /**
     * 删除-特殊事项申请
     */
    public void deleteFybxdTssxSq(String id){
        if (StringUtils.isBlank(id)){
            throw new BusinessException("参数为空！");
        }

        OaSpcyJcyFybxdtssxsq bean = spcyJcyFybxdtssxsqService.getById(id);
        if (bean == null){
            throw new BusinessException("没有相匹配的数据！");
        }
        spcyJcyFybxdtssxsqService.removeById(id);

        String log = LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(bean));
        spcyLogService.addLogs(OaLogConstant.LOGS_BX_BXSQ,log,bean.getFbxdid(),"删除特殊事项申请", OaLogConstant.LOGS_SYS_TYPE_SC);
    }

    /**
     * 更新报销单-特殊事项申请
     */
    private void updateBxdTssxsq(String fbxdid){
        if (StringUtils.isBlank(fbxdid)){
            return;
        }

        LambdaUpdateWrapper<OaSpcyJcyFybxd> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(OaSpcyJcyFybxd::getId, fbxdid);

        OaSpcyJcyFybxd updateBean = new OaSpcyJcyFybxd();
        updateBean.setFsfytssxsq(OaJcyFybxdConstant.FYBXD_SFYTSSXSQ_YES);
        spcyJcyFybxdService.update(updateBean, updateWrapper);

    }

    /**
     * 更新特殊事项申请报销单id
     */
    public void updatetssqbxdid(String fbxdid,String sxsqid){
        if (StringUtils.isBlank(fbxdid)){
            return;
        }
        List<OaSpcyJcyFybxdtssxsq> oaSpcyJcyFybxdtssxsqs = spcyJcyFybxdtssxsqService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdtssxsq>()
                .eq(OaSpcyJcyFybxdtssxsq::getFsxsqid, sxsqid));

        if (CollectionUtils.isNotEmpty(oaSpcyJcyFybxdtssxsqs)) {
              OaSpcyJcyFybxd oaSpcyJcyFybxd = spcyJcyFybxdService.getById(fbxdid);
              oaSpcyJcyFybxd.setFsfytssxsq(OaJcyFybxdConstant.FYBXD_SFYTSSXSQ_YES);
              spcyJcyFybxdService.updateById(oaSpcyJcyFybxd);
             for (OaSpcyJcyFybxdtssxsq updateBean : oaSpcyJcyFybxdtssxsqs) {
               updateBean.setFbxdid(fbxdid);
                spcyJcyFybxdtssxsqService.updateById(updateBean);
            }
        }

    }

    /**
     * 新增转账申请单-----费用报销单人记录
     */
    public Result addzzsq(NkRsZzsqd nkRsZzsqd){
        /**2024-5-26
         * 可以不在这里相加 重新写一个接口 组装成一条数据 返回
         */
//        BigDecimal allje = new BigDecimal(0);
//        if (StringUtils.isNotBlank(nkRsZzsqd.getFbxdid())){
//            List<NkRsZzsqd> nkRsZzsqds = nkRsZzzsqdService.list(new LambdaQueryWrapper<NkRsZzsqd>()
//                    .eq(NkRsZzsqd::getFskdw, nkRsZzsqd.getFskdw())
//                    .eq(NkRsZzsqd::getFbxdid, nkRsZzsqd.getFbxdid()));
//            if (CollectionUtils.isNotEmpty(nkRsZzsqds)){
//                //循环所有的数据 拿到金额 相加 虽然在这之后只有一个
//                for(NkRsZzsqd nkRsZzsqdss:nkRsZzsqds){
//                    String fjexx = nkRsZzsqdss.getFjexx();
//                    allje = allje.add(new BigDecimal(fjexx));
//                }
//                BigDecimal addje = allje.add(new BigDecimal(nkRsZzsqd.getFjexx()));
//                String id = nkRsZzsqds.get(0).getId();
//                NkRsZzsqd nkRsZzsqd1 = nkRsZzzsqdService.getById(id);
//                nkRsZzsqd1.setFjexx(String.valueOf(addje));
//                nkRsZzsqd1.setFjedx(NumberToChineseCapital.toChineseMoney(addje));
//
//            }
//
//
//        }


                NkRsZzsqd nkRsZzsqds = new NkRsZzsqd();
                nkRsZzsqds.setId(GetUuIdUtils.ReplaceUuId());
                nkRsZzsqds.setFbxdid(nkRsZzsqd.getFbxdid());
                nkRsZzsqds.setFsqsxid(nkRsZzsqd.getFsqsxid());
                nkRsZzsqds.setFskdw(nkRsZzsqd.getFskdw());
                nkRsZzsqds.setFsqrid(nkRsZzsqd.getFsqrid());
                nkRsZzsqds.setFzh(nkRsZzsqd.getFzh());
                nkRsZzsqds.setFkhyh(nkRsZzsqd.getFkhyh());
                nkRsZzsqds.setFjexx(nkRsZzsqd.getFjexx());
                nkRsZzsqds.setFjedx(NumberToChineseCapital.toChineseMoney(new BigDecimal(nkRsZzsqd.getFjexx())));
                nkRsZzsqds.setFyt(nkRsZzsqd.getFyt());
                nkRsZzsqds.setFwfqk(nkRsZzsqd.getFwfqk());
                nkRsZzsqds.setFfkfs(OaJcyFybxdConstant.FKFS_ZZ);
                nkRsZzsqds.setFsqrxm(ycSysUserService.getById(nkRsZzsqd.getFsqrid()).getRealname());
                nkRsZzsqds.setFbxdgrjlid(nkRsZzsqd.getFbxdgrjlid());
                nkRsZzsqds.setFjtxcdid(nkRsZzsqd.getFjtxcdid());
                nkRsZzzsqdService.save(nkRsZzsqds);

                return Result.ok("更新成功");
    }

    /**
     * 转账申请单查询
     */
    public Result selectZzsqd(String fbxdid) {
        List<NkRsZzsqd> list = nkRsZzzsqdService.list(new LambdaQueryWrapper<NkRsZzsqd>()
                .eq(NkRsZzsqd::getFbxdid, fbxdid));
        return Result.ok(list);
    }


    /**
     * 获取在职人员汇总表单
     */
    public Map getZzryBd(String bxdid) {
        Map map = new HashMap();

        //汇总表单
            //报销单
        OaSpcyJcyFybxd oaSpcyJcyFybxd = spcyJcyFybxdService.getById(bxdid);
            //报销单个人记录
        List<OaSpcyJcyFybxdgrjl> oaSpcyJcyFybxdgrjls = spcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, bxdid));

        map.put("bxd", oaSpcyJcyFybxd);
        map.put("bxdgr", oaSpcyJcyFybxdgrjls);

        //报销明细
        List<OaSpcyJcyFybxxmmxjl> nkRsYszbczjls = spcyJcyFybxxmmxkaslService.list(new LambdaQueryWrapper<OaSpcyJcyFybxxmmxjl>()
                .eq(OaSpcyJcyFybxxmmxjl::getFbxdid, bxdid));
        map.put("bxmx",nkRsYszbczjls);

        //预算项目
        if(StringUtils.isNotBlank(oaSpcyJcyFybxd.getFysxmid())){
            String fysxmid = oaSpcyJcyFybxd.getFysxmid();
            if (StringUtils.isBlank(fysxmid)){
                map.put("yszb",null);
            }else {
                List<String> ysxmlist2 = Arrays.asList(fysxmid.split(",|，"));
                List ysxmlist = new ArrayList<>();
                List<NkRsXmyszbb> nkRsXmyszbbs = nkRsXmyszbbService.listByIds(ysxmlist2);
                for(NkRsXmyszbb nkRsXmyszbb :nkRsXmyszbbs){
                    Map ysxmmap = new HashMap();
                    String fysxmmc = nkRsXmyszbb.getFysxmmc();
                    String fbm = nkRsXmyszbb.getFbm();
                    String fbmjjflmc = nkRsXmyszbb.getFbmjjflmc();
                    String fzbjy = nkRsXmyszbb.getFzbjy();
                    if (fbmjjflmc.equals(OaJcyFybxdConstant.BXXMFL_LWF)){
                        ysxmmap.put("fbxje", oaSpcyJcyFybxd.getFlwfbxje());
                    }else {
                        ysxmmap.put("fbxje", oaSpcyJcyFybxd.getFclfbxje());
                    }
                    ysxmmap.put("fysxmmc",fysxmmc);
                    ysxmmap.put("fbm",fbm);
                    ysxmmap.put("fbmjjflmc",fbmjjflmc);
                    ysxmmap.put("fzbjy",fzbjy);
                    ysxmmap.put("fzzb",nkRsXmyszbb.getFzzb());
                    ysxmmap.put("fzcjd",nkRsXmyszbb.getFzcjd());
                    ysxmlist.add(ysxmmap);
                }

                map.put("yszb",ysxmlist);
            }
        }else {
            map.put("yszb",null);
        }
        //关联业务
        String flcbm = oaSpcyJcyFybxd.getFlcbm();
        List sqsxlist = new ArrayList();
        List sqsxnrlist = new ArrayList();
        if (StringUtils.isBlank(flcbm)){
            map.put("sxsq","");
        }else if(flcbm.equals(OaFlowConstant.NK_CCSP)){
            String fsqsxid = oaSpcyJcyFybxd.getFsqsxid();
            List<String> sqsxid = Arrays.asList(fsqsxid.split(",|，"));
            for (String sqsxids :sqsxid){
                NkRsCcsp xzswsp = nkRsCcspService.getById(sqsxids);
                Map sqsxmap = new HashMap();
                sqsxmap.put("id",xzswsp.getId());
                sqsxmap.put("fxm",xzswsp.getFxm());
                sqsxmap.put("fsy",xzswsp.getFccsy());
                sqsxlist.add(sqsxmap);
                sqsxnrlist.add(xzswsp);
            }
            map.put("sxsq",sqsxlist);
            map.put("sxsqnr",sqsxnrlist);
        }else if (flcbm.equals(OaFlowConstant.OA_BGYPSQ)){
            String fsqsxid = oaSpcyJcyFybxd.getFsqsxid();
            List<String> sqsxid = Arrays.asList(fsqsxid.split(",|，"));
            List<OaBgypsqgl> oaBgypsqgls = oaBgypsqglService.listByIds(sqsxid);
            map.put("sxsq",oaBgypsqgls);
        }else if (flcbm.equals(OaFlowConstant.OA_XZSWSP)){
            String fsqsxid = oaSpcyJcyFybxd.getFsqsxid();
            List<String> sqsxid = Arrays.asList(fsqsxid.split(",|，"));
            for (String sqsxids :sqsxid){
                OaXzswspEntity xzswsp = oaxzswspService.getById(sqsxids);
                Map sqsxmap = new HashMap();
                sqsxmap.put("id",xzswsp.getId());
                sqsxmap.put("fxm",xzswsp.getFnbr());
                sqsxmap.put("fsy",xzswsp.getFsx());
                sqsxlist.add(sqsxmap);
                sqsxnrlist.add(xzswsp);
            }
            map.put("sxsq",sqsxlist);
            map.put("sxsqnr",sqsxnrlist);
        } else if (flcbm.equals(OaFlowConstant.NK_HTSPLC)) {
            String fsqsxid = oaSpcyJcyFybxd.getFsqsxid();
            List<String> sqsxid = Arrays.asList(fsqsxid.split(",|，"));
            for (String sqsxids :sqsxid){
                NkRsHtspjl nkRsHtspjl = nkRsHtspjlService.getById(sqsxids);
                Map sqsxmap = new HashMap();
                sqsxmap.put("id",nkRsHtspjl.getId());
                sqsxmap.put("fxm",nkRsHtspjl.getFtxr());
                sqsxmap.put("fsy",nkRsHtspjl.getFhtmc());
                sqsxlist.add(sqsxmap);
                sqsxnrlist.add(nkRsHtspjl);
            }
            map.put("sxsq",sqsxlist);
            map.put("sxsqnr",sqsxnrlist);
        } else if (flcbm.equals(OaFlowConstant.NK_JCXSWSQ)){
            String fsqsxid = oaSpcyJcyFybxd.getFsqsxid();
            List<String> sqsxid = Arrays.asList(fsqsxid.split(",|，"));
            for (String sqsxids :sqsxid){
                NkJcxswsq nkJcxswsq = nkJcxswsqService.getById(sqsxids);
                Map sqsxmap = new HashMap();
                sqsxmap.put("id",nkJcxswsq.getId());
                sqsxmap.put("fxm",nkJcxswsq.getFsqr());
                sqsxmap.put("fsy",nkJcxswsq.getFsqnr());
                sqsxmap.put("fsqnr",nkJcxswsq.getFsqnr());
                sqsxmap.put("fsqyj",nkJcxswsq.getFsqyj());
                sqsxmap.put("fsqsj", nkJcxswsq.getFsqsj());
                sqsxlist.add(sqsxmap);
                sqsxnrlist.add(nkJcxswsq);
            }
            map.put("sxsq",sqsxlist);
            map.put("jcxswsxsqnr",sqsxnrlist);
        }
        //特殊事项
        //特殊事项变为事前申请，所以根据业务id查询。初始化后在根据申请事项id 去查询特殊事项申请表有没有
        if (StringUtils.isNotBlank(oaSpcyJcyFybxd.getFbxdlx()) && oaSpcyJcyFybxd.getFbxdlx().equals(NkConstant.FYBXD_JCYBXD)){

                List<OaSpcyJcyFybxdtssxsq> list = spcyJcyFybxdtssxsqService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdtssxsq>()
                        .eq(OaSpcyJcyFybxdtssxsq::getFbxdid, bxdid));

                map.put("tssx",list);
        }else {
            List<OaSpcyJcyFybxdtssxsq> list = new ArrayList<>();
            String fsqsxid = oaSpcyJcyFybxd.getFsqsxid();
            List<String> sqsxid = Arrays.asList(fsqsxid.split(",|，"));
            for (String fsqsxid1 : sqsxid){
                list = spcyJcyFybxdtssxsqService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdtssxsq>()
                        .eq(OaSpcyJcyFybxdtssxsq::getFsxsqid, fsqsxid1));
            }
            map.put("tssx",list);


            //摘要list 好像需要
            List<NkRsBxqtzyjl> nkRsBxqtzyjls = nkRsBxqtzyjlService.list(new LambdaQueryWrapper<NkRsBxqtzyjl>()
                    .eq(NkRsBxqtzyjl::getFbxdid, bxdid));
            if (CollectionUtils.isNotEmpty(nkRsYszbczjls)){
                map.put("qtzy",nkRsBxqtzyjls);
            }else {
                map.put("qtzy",null);
            }
        }



        //申请转账单
        List<NkRsZzsqd> list1 = nkRsZzzsqdService.list(new LambdaQueryWrapper<NkRsZzsqd>()
                .eq(NkRsZzsqd::getFbxdid, bxdid));
        map.put("sqzzt",list1);


        //如果是合同，查询合同的的里面的绑定id，在查询到行政的数据
        String fsqsxid = oaSpcyJcyFybxd.getFsqsxid();
        if (StringUtils.isNotBlank(fsqsxid)){
            NkRsHtspjl nthtsp = nkRsHtspjlService.getById(fsqsxid);
            if (nthtsp != null){
                String fxzswid = nthtsp.getFxzswid();
                if (StringUtils.isNotBlank(fxzswid)){
                    OaXzswspEntity xzswspEntity = oaxzswspService.getById(fxzswid);
                    map.put("xzswsp", xzswspEntity);
                }

            }

        }

        return map;
    }


    /**
     * 根据最后汇总更新报销单，个人表单记录每个人的公务卡 和 转账需求
     */
    public void gethzdByBxd(String bxdid){
        OaSpcyJcyFybxd oaSpcyJcyFybxd = spcyJcyFybxdService.getById(bxdid);
        List<OaSpcyJcyFybxdgrjl> jcyFybxdgrjls = spcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, bxdid));

        //--初始化
        //交通费小计
        BigDecimal jtfxj = new BigDecimal(0);
        //出差费小计
        BigDecimal ccfxj = new BigDecimal(0);
        //其他小计
        BigDecimal qtxj = new BigDecimal(0);

        //住宿费小计
        BigDecimal zsfxj = new BigDecimal(0);

        //公务卡报销金额
        BigDecimal gwkbxje = new BigDecimal(0);
        //转账报销金额
        BigDecimal zzbxje = new BigDecimal(0);
        //储蓄卡报销金额
        BigDecimal cxkbxje = new BigDecimal(0);

        //合计报销金额
        BigDecimal hjbxje = new BigDecimal(0);

        if (CollectionUtils.isNotEmpty(jcyFybxdgrjls)){
            for (OaSpcyJcyFybxdgrjl je:jcyFybxdgrjls){
                //交通费小计
//                jtfxj = jtfxj.add(new BigDecimal(Optional.ofNullable(je.getFwjtfsbje()).orElseGet(() -> String.valueOf(0))));
//                jtfxj = jtfxj.add(new BigDecimal(Optional.ofNullable(je.getFfjtfsbje()).orElseGet(() -> String.valueOf(0))));

                List<NkBxJtxcd> nkBxJtxcds = nkBxJtxcdService.list(new LambdaQueryWrapper<NkBxJtxcd>()
                        .eq(NkBxJtxcd::getFbxdgrid, je.getId()));
                for (NkBxJtxcd nkBxJtxcd:nkBxJtxcds){

                        jtfxj = jtfxj.add(new BigDecimal(Optional.ofNullable(nkBxJtxcd.getFwjtfybje()).orElseGet(()->"0")));

                }

                //出差费小计
                ccfxj = ccfxj.add(new BigDecimal(Optional.ofNullable(je.getFccbzje()).orElseGet(() -> String.valueOf(0))));

                //其他小计
//                qtxj = qtxj.add(new BigDecimal(Optional.ofNullable(je.getFqtje()).orElseGet(() -> String.valueOf(0))));

                OaSpcyJcyFybxdgrjl bxdgrl =  spcyJcyFybxdgrjlService.getById(je.getId());

                //这里循环 其他小计和其他摘要
                List<NkRsBxqtzyjl> nkRsBxqtzyjls = nkRsBxqtzyjlService.list(new LambdaQueryWrapper<NkRsBxqtzyjl>()
                        .eq(NkRsBxqtzyjl::getFbxdgrjl, je.getId()));
                if (CollectionUtils.isNotEmpty(nkRsBxqtzyjls)){
                    BigDecimal grqtxj = new BigDecimal(0);
                    String qtzy = "";
                    for (NkRsBxqtzyjl nkRsBxqtzyjl : nkRsBxqtzyjls){

                        qtxj = qtxj.add(new BigDecimal(Optional.ofNullable(nkRsBxqtzyjl.getFqtje()).orElseGet(() -> String.valueOf(0))));

                        grqtxj = grqtxj.add(new BigDecimal(Optional.ofNullable(nkRsBxqtzyjl.getFqtje()).orElseGet(() -> String.valueOf(0))));

                        qtzy += nkRsBxqtzyjl.getFqtzy() + ":" + nkRsBxqtzyjl.getFqtje() + "元,";


                    }

                bxdgrl.setFqtzy(qtzy);
                bxdgrl.setFqtje(String.valueOf(grqtxj));
                spcyJcyFybxdgrjlService.updateById(bxdgrl);
                }else{
                    BigDecimal grqtxj = new BigDecimal(0);
                    grqtxj = (new BigDecimal(String.valueOf(0)));
                    bxdgrl.setFqtje(String.valueOf(grqtxj));
                }
                //住宿费小计
                zsfxj = zsfxj.add(new BigDecimal(Optional.ofNullable(je.getFzxfsbje()).orElseGet(() -> String.valueOf(0))));

                //公务卡报销金额
                gwkbxje = gwkbxje.add(new BigDecimal(Optional.ofNullable(je.getFgwkbxje()).orElseGet(() -> String.valueOf(0))));

                //转账报销金额
                zzbxje = zzbxje.add(new BigDecimal(Optional.ofNullable(je.getFzzbxje()).orElseGet(() -> String.valueOf(0))));

                //储蓄卡报销金额
                cxkbxje = cxkbxje.add(new BigDecimal(Optional.ofNullable(je.getFcxkbxje()).orElseGet(() -> String.valueOf(0))));

            }
            //小计
            oaSpcyJcyFybxd.setFjtfxj(String.valueOf(jtfxj));
            oaSpcyJcyFybxd.setFccbzxj(String.valueOf(ccfxj));
            oaSpcyJcyFybxd.setFqtxj(String.valueOf(qtxj));
            oaSpcyJcyFybxd.setFzsfxj(String.valueOf(zsfxj));

            //卡
            oaSpcyJcyFybxd.setFgwkbxje(String.valueOf(gwkbxje));
            oaSpcyJcyFybxd.setFzzbxje(String.valueOf(zzbxje));
            oaSpcyJcyFybxd.setFcxkbxje(String.valueOf(cxkbxje));


            spcyJcyFybxdService.updateById(oaSpcyJcyFybxd);
        }
    }



    /**
     * 更新个人 金额记录
     */
    public void updategrje(String bxdid){
        //查询当前报销单下，所有的个人记录
        List<OaSpcyJcyFybxdgrjl> jcyFybxdgrjls = spcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, bxdid));
        //如果不为空，那就循环。判断是否有转账和公务卡报销，如果有就将对应的数据加到转账报销金额 和公务卡报销金额。否则放到储蓄卡
        if (CollectionUtils.isNotEmpty(jcyFybxdgrjls)){
            for (OaSpcyJcyFybxdgrjl fybxdgrjl :jcyFybxdgrjls){

                //初始化公务卡报销金额
                BigDecimal gwkbxje = new BigDecimal(0);
                //初始化转账金额
                BigDecimal zzje = new BigDecimal(0);
                //初始化储蓄卡报销金额
                BigDecimal cskbxje = new BigDecimal(0);


                //出差补助放进储蓄卡
                if (StringUtils.isNotBlank(fybxdgrjl.getFccbzje()) || StringUtils.isNotBlank(fybxdgrjl.getFqtje())){
                    BigDecimal ccbzje = new BigDecimal(Optional.ofNullable(fybxdgrjl.getFccbzje()).orElseGet(() -> String.valueOf(0)));
                    cskbxje = cskbxje.add(ccbzje);

//                    BigDecimal qtje = new BigDecimal(Optional.ofNullable(fybxdgrjl.getFqtje()).orElseGet(() -> String.valueOf(0)));
//                    cskbxje = cskbxje.add(qtje);
                    //其他金额按照其他金额表放进公务卡或者储蓄卡
                    List<NkRsBxqtzyjl> nkRsBxqtzyjls = nkRsBxqtzyjlService.list(new LambdaQueryWrapper<NkRsBxqtzyjl>()
                            .eq(NkRsBxqtzyjl::getFbxdgrjl, fybxdgrjl.getId()));

                    if (CollectionUtils.isNotEmpty(nkRsBxqtzyjls)){
                        for (NkRsBxqtzyjl nkRsBxqtzyjl:nkRsBxqtzyjls){
                            String fqtje = nkRsBxqtzyjl.getFqtje();
                            if (nkRsBxqtzyjl.getFsfgwkfk().equals(OaCommonFlowConstant.SFGWKFK_YES)){
                                gwkbxje = gwkbxje.add(new BigDecimal(fqtje));
                            }else {
                                cskbxje = cskbxje.add(new BigDecimal(fqtje));
                            }

                        }
                    }


                }
                //特殊事项申请也放进储蓄卡
                List<OaSpcyJcyFybxdtssxsq> fybxdtssxsqs = spcyJcyFybxdtssxsqService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdtssxsq>()
                        .eq(OaSpcyJcyFybxdtssxsq::getFbxdid, bxdid)
                        .eq(OaSpcyJcyFybxdtssxsq:: getFlczt, OaFlowConstants.HJMC_LCJS));
                if (CollectionUtils.isNotEmpty(fybxdtssxsqs)){
                    for (OaSpcyJcyFybxdtssxsq sq : fybxdtssxsqs){
                        cskbxje = cskbxje.add(new BigDecimal(Optional.ofNullable(sq.getFsqje()).orElseGet(()->"0")));
                    }
                }

//                //判断车旅费 应该放在哪里
//                if (StringUtils.isNotBlank(fybxdgrjl.getFsfzz()) || StringUtils.isNotBlank(fybxdgrjl.getFsfgwkdfclff())
//                        || StringUtils.isNotBlank(fybxdgrjl.getFsfgwkdfclfw())){
//                    //如果转账为否 ，往和返哪个垫付是是。哪个就将车旅费放到公务卡报销金额。转账为是，那就将车旅费放到转账报销金额
//                    if(fybxdgrjl.getFsfzz().equals(JcyFybxdConstant.FKFS_ZZ_YES)){
//                        List<NkRsZzsqd> rsZzsqds = nkRsZzzsqdService.list(new LambdaQueryWrapper<NkRsZzsqd>()
//                                .eq(NkRsZzsqd::getFbxdid, fybxdgrjl.getFbxdid())
//                                .eq(NkRsZzsqd::getFsqsxid, fybxdgrjl.getFsqsxid())
//                                .eq(NkRsZzsqd::getFsqrid, fybxdgrjl.getFuserid()));
//                        //不为空循环这个申请单 ，如果往返情况为去程 则继续判断返程的情况是否用公务卡报销是就加 不是就加到储蓄卡
//                        //不为空循环
//                        if (CollectionUtils.isNotEmpty(rsZzsqds)){
//                            for (NkRsZzsqd zzsqd:rsZzsqds){
//                                //如果是一条数据则需要判断是去程 返程 还是全程，
//                                //如果是去程 返程 那就判断是否公务卡垫付，如果公务卡垫付为否，那就放到转账申请内。
//                                if (rsZzsqds.size() == 1) {
//                                    if (zzsqd.getFwfqk().equals(JcyFybxdConstant.ZZWFQK_QC)) {
//
//                                        if (fybxdgrjl.getFsfgwkdfclff().equals(JcyFybxdConstant.DFCF_YES)) {
//                                            gwkbxje = gwkbxje.add(new BigDecimal(Optional.ofNullable(fybxdgrjl.getFfjtfsbje()).orElseGet(() -> String.valueOf(0))));
//                                        } else {
//                                            zzje = zzje.add(new BigDecimal(zzsqd.getFjexx()));
//                                        }
//
//                                    }
//
//                                    if (zzsqd.getFwfqk().equals(JcyFybxdConstant.ZZWFQK_FC)) {
//
//                                        if (fybxdgrjl.getFsfgwkdfclfw().equals(JcyFybxdConstant.DFCF_YES)) {
//                                            gwkbxje = gwkbxje.add(new BigDecimal(Optional.ofNullable(fybxdgrjl.getFwjtfsbje()).orElseGet(() -> String.valueOf(0))));
//                                        } else {
//                                            zzje = zzje.add(new BigDecimal(zzsqd.getFjexx()));
//                                        }
//                                    }
//
//                                    if (zzsqd.getFwfqk().equals(JcyFybxdConstant.ZZWFQK_QQC)) {
//                                        zzje = zzje.add(new BigDecimal(zzsqd.getFjexx()));
//                                    }
//
//                                }
//                                //如果一个人查询出两条转账申请单，那么就是去程 和 往程 不是一个收款单位
//                                if (rsZzsqds.size() == 2){
//                                    zzje = zzje.add(new BigDecimal(zzsqd.getFjexx()));
//                                }
//
//                            }
//                        }
//
//                    } else {
//                        //判断 往返是否使用公务卡 如果不使用，直接放进储蓄卡
//                        //返程
//                        if (fybxdgrjl.getFsfgwkdfclff().equals(JcyFybxdConstant.DFCF_YES)){
//                            gwkbxje = gwkbxje.add(new BigDecimal(Optional.ofNullable(fybxdgrjl.getFfjtfsbje()).orElseGet(() -> String.valueOf(0))));
//                        }else {
//                            cskbxje = cskbxje.add(new BigDecimal(Optional.ofNullable(fybxdgrjl.getFfjtfsbje()).orElseGet(() -> String.valueOf(0))));
//                        }
//                        //去程
//                        if (fybxdgrjl.getFsfgwkdfclfw().equals(JcyFybxdConstant.DFCF_YES)){
//                            gwkbxje = gwkbxje.add(new BigDecimal(Optional.ofNullable(fybxdgrjl.getFwjtfsbje()).orElseGet(() -> String.valueOf(0))));
//                        }else {
//                            cskbxje = cskbxje.add(new BigDecimal(Optional.ofNullable(fybxdgrjl.getFwjtfsbje()).orElseGet(() -> String.valueOf(0))));
//                        }
//                    }
//                }


                List<NkBxJtxcd> nkBxJtxcds = nkBxJtxcdService.list(new LambdaQueryWrapper<NkBxJtxcd>()
                        .eq(NkBxJtxcd::getFbxdgrid, fybxdgrjl.getId()));
//                BigDecimal fjje = new BigDecimal(0);
                //在循环

                for (NkBxJtxcd nkBxJtxcd:nkBxJtxcds){
                    //判断出行方式，如果为飞机 ，并且是转帐为是 将应报金额相加 然后更新进 报销单个人的转账申请字段内
                    if (nkBxJtxcd.getFcsjjtfs().equals(OaJcyFybxdConstant.CXFS_FJ) && nkBxJtxcd.getFsfzz().equals(OaFlowConstant.YES)){
                        //在职没有应报 只有实报
                        zzje = zzje.add(new BigDecimal(nkBxJtxcd.getFwjtfybje()));
                        fybxdgrjl.setFzzbxje(String.valueOf(zzje));

                    }else if (nkBxJtxcd.getFsfgwkbx().equals(OaFlowConstant.YES)){
                        gwkbxje = gwkbxje.add(new BigDecimal(nkBxJtxcd.getFwjtfybje()));
                        nkBxJtxcd.setFwjtfsbje("0");
                        fybxdgrjl.setFgwkbxje(String.valueOf(zzje));

                    }else {
                        cskbxje = cskbxje.add(new BigDecimal(Optional.ofNullable(nkBxJtxcd.getFwjtfsbje()).orElseGet(()->"0")));
                        fybxdgrjl.setFcxkbxje(String.valueOf(zzje));
                    }

                }


                //判断住宿费放在哪里
                if(StringUtils.isNotBlank(fybxdgrjl.getFzxfsbje())){

                    if (fybxdgrjl.getFsfgwkfzsf().equals(OaJcyFybxdConstant.DFZSF_YES)){
                        gwkbxje = gwkbxje.add(new BigDecimal(Optional.ofNullable(fybxdgrjl.getFzxfsbje()).orElseGet(() -> String.valueOf(0))));
                    }else {
                        cskbxje = cskbxje.add(new BigDecimal(Optional.ofNullable(fybxdgrjl.getFzxfsbje()).orElseGet(() -> String.valueOf(0))));
                    }
                }
                fybxdgrjl.setFgwkbxje(String.valueOf(gwkbxje));
                fybxdgrjl.setFzzbxje(String.valueOf(zzje));
                fybxdgrjl.setFcxkbxje(String.valueOf(cskbxje));
                spcyJcyFybxdgrjlService.updateById(fybxdgrjl);
            }

        }

    }


    /**
    * 更新交通费转账申请之后当前人的交通费状态 将交通为飞机的金额 放进 个人报销单的转账申请金额
     *
     *
    */
//    public void updatejczt(String fbxdid){
//
//        List<SpcyJcyFybxdgrjl> jcyFybxdgrjls = spcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<SpcyJcyFybxdgrjl>()
//                .eq(SpcyJcyFybxdgrjl::getFbxdid, fbxdid));
//
//
//
//        //如果不为空，则将此条记录的 往返交通费设置为转账申请单的金额 然后将垫付人 更新为申请转账单的收款单位 并将其金额放进转账报销金额内
//        if (CollectionUtils.isNotEmpty(jcyFybxdgrjls)) {
//            for (SpcyJcyFybxdgrjl spcyJcyFybxdgrjl : jcyFybxdgrjls){
//
//
//                if (StringUtils.isNotBlank(spcyJcyFybxdgrjl.getFcsjjtfs()) && spcyJcyFybxdgrjl.getFcsjjtfs().equals(JcyFybxdConstant.CXFS_FJ) ||
//                    StringUtils.isNotBlank(spcyJcyFybxdgrjl.getFcsjjtfsf()) && spcyJcyFybxdgrjl.getFcsjjtfsf().equals(JcyFybxdConstant.CXFS_FJ)) {
//                    //查询当前申请人的转账申请记录
//                    List<NkRsZzsqd> rsZzsqds = nkRsZzzsqdService.list(new LambdaQueryWrapper<NkRsZzsqd>()
//                            .eq(NkRsZzsqd::getFbxdid, spcyJcyFybxdgrjl.getFbxdid())
//                            .eq(NkRsZzsqd::getFsqsxid, spcyJcyFybxdgrjl.getFsqsxid())
//                            .eq(NkRsZzsqd::getFsqrid, spcyJcyFybxdgrjl.getFuserid()));
//                    //如果有两条申请记录，说明是两个单位的
//                    if (rsZzsqds.size() == 2) {
//                        for (NkRsZzsqd zzsqd : rsZzsqds) {
//                            BigDecimal wjtf = new BigDecimal(0);
//                            BigDecimal fjtf = new BigDecimal(0);
//                            //去程
//                            if (zzsqd.getFwfqk().equals(JcyFybxdConstant.ZZWFQK_QC)) {
//                                wjtf = wjtf.add(new BigDecimal(zzsqd.getFjexx()));
//
//                                spcyJcyFybxdgrjl.setFwjtfsbje(String.valueOf(wjtf));
//                                spcyJcyFybxdgrjl.setFwjtfsfdf(JcyFybxdConstant.DFCF_YES);
//                                spcyJcyFybxdgrjl.setFwjtfdfrxm(zzsqd.getFskdw());
//                            }
//                            //返程
//                            if (zzsqd.getFwfqk().equals(JcyFybxdConstant.ZZWFQK_FC)) {
//                                fjtf = fjtf.add(new BigDecimal(zzsqd.getFjexx()));
//                                spcyJcyFybxdgrjl.setFfjtfsbje(String.valueOf(fjtf));
//
//                                spcyJcyFybxdgrjl.setFfjtfsfdf(JcyFybxdConstant.DFCF_YES);
//                                spcyJcyFybxdgrjl.setFfjtfdfrxm(zzsqd.getFskdw());
//                            }
//                            BigDecimal zzbxje = wjtf.add(fjtf);
//                            spcyJcyFybxdgrjl.setFzzbxje(String.valueOf(zzbxje));
//                            spcyJcyFybxdgrjlService.updateById(spcyJcyFybxdgrjl);
//
//                        }
//                    } else if (rsZzsqds.size() == 1) {
//                        for (NkRsZzsqd zzsqd : rsZzsqds) {
//                            BigDecimal wjtf = new BigDecimal(0);
//                            BigDecimal fjtf = new BigDecimal(0);
//                            //全程，如果是全程，没有必要再去判断去程和返程
//                            if (zzsqd.getFwfqk().equals(JcyFybxdConstant.ZZWFQK_QQC)){
//                                //那么往交通费和返交通费的数值怎么算,放在往程就行
//                                fjtf = fjtf.add(new BigDecimal(zzsqd.getFjexx()));
//                                spcyJcyFybxdgrjl.setFwjtfsbje(String.valueOf(fjtf));
//
//                                spcyJcyFybxdgrjl.setFwjtfsfdf(JcyFybxdConstant.DFCF_YES);
//                                spcyJcyFybxdgrjl.setFfjtfsfdf(JcyFybxdConstant.DFCF_YES);
//
//
//                                spcyJcyFybxdgrjl.setFcsjjtfsf(JcyFybxdConstant.CXFS_FJ);
//
//                                spcyJcyFybxdgrjl.setFwjtfdfrxm(zzsqd.getFskdw());
//                                spcyJcyFybxdgrjl.setFfjtfdfrxm(zzsqd.getFskdw());
//                                spcyJcyFybxdgrjl.setFzzbxje(String.valueOf(fjtf));
//
//
//                            }else {
//                                //去程
//                                if (zzsqd.getFwfqk().equals(JcyFybxdConstant.ZZWFQK_QC)) {
//                                    wjtf = wjtf.add(new BigDecimal(zzsqd.getFjexx()));
//                                    spcyJcyFybxdgrjl.setFwjtfsbje(String.valueOf(wjtf));
//
//                                    spcyJcyFybxdgrjl.setFwjtfsfdf(JcyFybxdConstant.DFCF_YES);
//                                    spcyJcyFybxdgrjl.setFwjtfdfrxm(zzsqd.getFskdw());
//                                    spcyJcyFybxdgrjl.setFzzbxje(String.valueOf(wjtf));
//                                }
//                                //返程
//                                if (zzsqd.getFwfqk().equals(JcyFybxdConstant.ZZWFQK_FC)) {
//                                    fjtf = fjtf.add(new BigDecimal(zzsqd.getFjexx()));
//                                    spcyJcyFybxdgrjl.setFfjtfsbje(String.valueOf(fjtf));
//
//                                    spcyJcyFybxdgrjl.setFfjtfsfdf(JcyFybxdConstant.DFCF_YES);
//                                    spcyJcyFybxdgrjl.setFfjtfdfrxm(zzsqd.getFskdw());
//                                    spcyJcyFybxdgrjl.setFzzbxje(String.valueOf(fjtf));
//                                }
//                            }
//                            spcyJcyFybxdgrjlService.updateById(spcyJcyFybxdgrjl);
//
//                        }
//                    }
//                }
//            }
//        }
//    }


    /**
     * 更新交通费转账申请之后当前人的交通费状态 将交通为飞机的金额 放进 个人报销单的转账申请金额
     *
     *
     */
    public void updatejczt(String fbxdid){
        //在职没有应报 只有实报
                //将交通行程单的 出行方式为 飞机的 并是否转帐 为是的 飞机金额 放进个人的转账申请金额内
        //有飞机 但是公务卡报销的 将飞机的金额 放进公务卡

        List<OaSpcyJcyFybxdgrjl> jcyFybxdgrjls = spcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, fbxdid));

        for (OaSpcyJcyFybxdgrjl jcyFybxdgrjl:jcyFybxdgrjls){
            List<NkBxJtxcd> nkBxJtxcds = nkBxJtxcdService.list(new LambdaQueryWrapper<NkBxJtxcd>()
                    .eq(NkBxJtxcd::getFbxdgrid, jcyFybxdgrjl.getId()));
            BigDecimal fjje = new BigDecimal(0);
            //在循环

            for (NkBxJtxcd nkBxJtxcd:nkBxJtxcds){
                //判断出行方式，如果为飞机 ，并且是转帐为是 将应报金额相加 然后更新进 报销单个人的转账申请字段内
                if (nkBxJtxcd.getFcsjjtfs().equals(OaJcyFybxdConstant.CXFS_FJ) && nkBxJtxcd.getFsfzz().equals(OaFlowConstant.YES)){
                    //在职没有应报 只有实报
                    fjje = fjje.add(new BigDecimal(nkBxJtxcd.getFwjtfybje()));
                    jcyFybxdgrjl.setFzzbxje(String.valueOf(fjje));

                }else if (nkBxJtxcd.getFsfgwkbx().equals(OaFlowConstant.YES)){
                    fjje = fjje.add(new BigDecimal(nkBxJtxcd.getFwjtfybje()));
                    nkBxJtxcd.setFwjtfsbje("0");
                    jcyFybxdgrjl.setFgwkbxje(String.valueOf(fjje));

                }else {
                    fjje = fjje.add(new BigDecimal(Optional.ofNullable(nkBxJtxcd.getFwjtfsbje()).orElseGet(()->"0")));
                    jcyFybxdgrjl.setFcxkbxje(String.valueOf(fjje));
                }

            }
            spcyJcyFybxdgrjlService.updateById(jcyFybxdgrjl);

        }


    }


    /**
     * 获取预览数据
     */
    public Map getYlsj(String bxdid,String sqsxid) {
        OaSpcyJcyFybxd oaSpcyJcyFybxd = spcyJcyFybxdService.getById(bxdid);
        String flcbm = oaSpcyJcyFybxd.getFlcbm();
        String jobid = null;
        String endjobid = null;
        Map map = new HashMap();
        if (flcbm.equals(OaFlowConstant.NK_CCSP)){
            List<OaJobEntity> jobList = spcySqsxJobService.list(new LambdaQueryWrapper<OaJobEntity>()
                    .eq(OaJobEntity::getFpid, sqsxid));
            for (OaJobEntity job : jobList){
                jobid = job.getId();
                List<OaJobEntity> endlist = spcySqsxJobService.list(new LambdaQueryWrapper<OaJobEntity>()
                        .eq(OaJobEntity::getFsyhjid, jobid));
                if(CollectionUtils.isEmpty(endlist)){
                    endjobid = jobid;
                }
            }
            map.put("jobid",endjobid);
            map.put("flcmb",OaFlowConstant.NK_CCSP);
            map.put("fpid",sqsxid);
        } else if (flcbm.equals(OaFlowConstant.OA_BGYPSQ)) {
            List<OaJobEntity> jobList = spcySqsxJobService.list(new LambdaQueryWrapper<OaJobEntity>()
                    .eq(OaJobEntity::getFpid, sqsxid));
            for (OaJobEntity job : jobList){
                jobid = job.getId();
                List<OaJobEntity> endlist = spcySqsxJobService.list(new LambdaQueryWrapper<OaJobEntity>()
                        .eq(OaJobEntity::getFsyhjid, jobid));
                if(CollectionUtils.isEmpty(endlist)){
                    endjobid = jobid;
                }
            }
            map.put("jobid",endjobid);
            map.put("flcmb",OaFlowConstant.OA_BGYPSQ);
            map.put("fpid",sqsxid);

        } else if (flcbm.equals(OaFlowConstant.OA_XZSWSP)) {
            List<OaJobEntity> jobList = spcySqsxJobService.list(new LambdaQueryWrapper<OaJobEntity>()
                    .eq(OaJobEntity::getFpid, sqsxid));
            for (OaJobEntity job : jobList){
                jobid = job.getId();
                List<OaJobEntity> endlist = spcySqsxJobService.list(new LambdaQueryWrapper<OaJobEntity>()
                        .eq(OaJobEntity::getFsyhjid, jobid));
                if(CollectionUtils.isEmpty(endlist)){
                    endjobid = jobid;
                }
            }
            map.put("jobid",endjobid);
            map.put("flcmb",OaFlowConstant.OA_XZSWSP);
            map.put("fpid",sqsxid);

        }else if (flcbm.equals(OaFlowConstant.NK_HTSPLC)){
            List<OaJobEntity> jobList = spcySqsxJobService.list(new LambdaQueryWrapper<OaJobEntity>()
                    .eq(OaJobEntity::getFpid, sqsxid));
            for (OaJobEntity job : jobList){
                jobid = job.getId();
                List<OaJobEntity> endlist = spcySqsxJobService.list(new LambdaQueryWrapper<OaJobEntity>()
                        .eq(OaJobEntity::getFsyhjid, jobid));
                if(CollectionUtils.isEmpty(endlist)){
                    endjobid = jobid;
                }
            }
            map.put("jobid",endjobid);
            map.put("flcmb",OaFlowConstant.NK_HTSPLC);
            map.put("fpid",sqsxid);
        }else if (flcbm.equals(OaFlowConstant.NK_JCXSWSQ)){
            map.put("jobid",null);
            map.put("flcmb",OaFlowConstant.NK_JCXSWSQ);
            map.put("fpid",sqsxid);
        }

        return map;
    }



    //删除申请依据/清空个人申请申请单记录
    public void deletesqyj(String bxdid){
        OaSpcyJcyFybxd oaSpcyJcyFybxd = spcyJcyFybxdService.getById(bxdid);
        String fsqsxid = oaSpcyJcyFybxd.getFsqsxid();


        if (StringUtils.isNotBlank(oaSpcyJcyFybxd.getFsqsxid())){
            oaSpcyJcyFybxd.setFsqsxid("");
        }
        if(StringUtils.isNotBlank(oaSpcyJcyFybxd.getFsqyj())){
            oaSpcyJcyFybxd.setFsqyj("");
        }
        if (StringUtils.isNotBlank(oaSpcyJcyFybxd.getFbxnrms())){
            oaSpcyJcyFybxd.setFbxnrms("");
        }

        if (StringUtils.isNotBlank(oaSpcyJcyFybxd.getFjbr())){
            oaSpcyJcyFybxd.setFjcqymc("");

            oaSpcyJcyFybxd.setFlxdh("");
            oaSpcyJcyFybxd.setFbxsjms("");
            oaSpcyJcyFybxd.setFjcsxmc("");
            oaSpcyJcyFybxd.setFjcksrq("");
            oaSpcyJcyFybxd.setFjcjsrq("");
            oaSpcyJcyFybxd.setFjcts(null);
        }

        spcyJcyFybxdService.updateById(oaSpcyJcyFybxd);

        List<OaSpcyJcyFybxdgrjl> jcyFybxdgrjls = spcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, bxdid));
        if (CollectionUtils.isNotEmpty(jcyFybxdgrjls)){
            for (OaSpcyJcyFybxdgrjl jcyFybxdgrjl:jcyFybxdgrjls){
                spcyJcyFybxdgrjlService.removeById(jcyFybxdgrjl);
            }
            OaSpcyJcyFybxdgrjl oaSpcyJcyFybxdgrjl = jcyFybxdgrjls.get(0);


            String flcbm = oaSpcyJcyFybxd.getFlcbm();
            if (flcbm.equals(OaFlowConstant.NK_CCSP)){
                NkRsCcsp ccsp = nkRsCcspService.getById(fsqsxid);
                ccsp.setFbxzt(NkConstant.BXZT_WBX);
                nkRsCcspService.updateById(ccsp);
            }else if (flcbm.equals(OaFlowConstant.OA_XZSWSP)){
                OaXzswspEntity xzswsp = oaxzswspService.getById(fsqsxid);
                xzswsp.setFbxzt(NkConstant.BXZT_WBX);
                oaxzswspService.updateById(xzswsp);
            }else if (flcbm.equals(OaFlowConstant.NK_HTSPLC)){
                NkRsHtspjl htspLc = nkRsHtspjlService.getById(fsqsxid);
                htspLc.setFbxzt(NkConstant.BXZT_WBX);
                nkRsHtspjlService.updateById(htspLc);
            }else if (flcbm.equals(OaFlowConstant.NK_JCXSWSQ)){
                NkJcxswsq nkJcxswsq = nkJcxswsqService.getById(fsqsxid);
                nkJcxswsq.setFsfysq(NkConstant.SQZT_WSQ);
                nkJcxswsqService.updateById(nkJcxswsq);
            }

        }
    }






}
